进程间通信—消息队列(一)

消息队列基本概念:

消息队列是系统内核地址空间中的一个内部的链表。消息可以按照顺序发送到队列中,

也可以以几种不同的方式从队列中读取。每一个消息队列用一个唯一的IPC标识符表示。

我们来实现一个简单的消息队列的工具,用于创建消息队列、发送、读取消息、

改变权限以及删除消息队列。

综合实例:msgtool

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define MAX_SEND_SIZE 80
struct mymsgbuf
{
	long mtype;
	char mtext[MAX_SEND_SIZE];
};
void send_message(int qid,struct mymsgbuf *qbuf,long type,char *text);
void read_message(int qid,struct mymsgbuf *qbuf,long type);
void remove_queue(int qid);
void change_queue_mode(int qid,char *mode);
void usage(void);
int main(int argc,char **argv)
{
	key_t key;
	int msgqueue_id;
	struct mymsgbuf qbuf;
	if(argc == 1)
	{
		usage();
	}
	key=ftok(".",'m');
	if((msgqueue_id = msgget(key,IPC_CREAT|0777))==-1)
	{
		perror("msgget");
		exit(1);
	}
	printf("message queue id=[%d]\n",msgqueue_id);
	switch(tolower(argv[1][0]))
	{
		case 's':
			if(argc < 4)
			{
				usage();
				break;
			}
			send_message(msgqueue_id,(struct mymsgbuf *)&qbuf,atol(argv[2]),argv[3]);
			break;
		case 'r':
			if(argc < 3)
			{
				usage();
				break;
			}
			read_message(msgqueue_id,&qbuf,atol(argv[2]));
			break;
		case 'd':
			remove_queue(msgqueue_id);
			break;
			case 'm':
			if(argc < 3)
			{
				usage();
				break;
			}
			change_queue_mode(msgqueue_id,argv[2]);
			break;
		default:
			usage();
	}
	return 0;
}
void send_message(int qid,struct mymsgbuf *qbuf,long type,char *text)
{
	printf("Sending a message...\n");
	qbuf->mtype=type;
	strcpy(qbuf->mtext,text);
	if((msgsnd(qid,(struct msgbuf *)qbuf,strlen(qbuf->mtext)+1,0))==-1)
	{
		perror("msgsnd");
		exit(0);
	}
}
void read_message(int qid,struct mymsgbuf *qbuf,long type)
{
	printf("Reading a message...\n");
	qbuf->mtype=type;
	msgrcv(qid,(struct msgbuf *)qbuf,MAX_SEND_SIZE,type,0);
	printf("Type:%ld Text:%s\n",qbuf->mtype,qbuf->mtext);
}
void remove_queue(int qid)
{
	msgctl(qid,IPC_RMID,0);
}

void change_queue_mode(int qid,char *mode)
{
	struct msqid_ds myqueue_ds;
	msgctl(qid,IPC_STAT,&myqueue_ds);
	sscanf(mode,"%ho",&myqueue_ds.msg_perm.mode);
	msgctl(qid,IPC_SET,&myqueue_ds);
}
void usage(void)
{
	fprintf(stderr,"msgtool -A utility for tinking with msgqueue\n");
	fprintf(stderr,"USAGE:msgtool (s)end <type> <messagetext>\n");
	fprintf(stderr,"(r)ecv <type>\n");
	fprintf(stderr,"(d)elete\n");
	fprintf(stderr,"(m)ode <octal mode>\n");
	exit(1);
}



一个终端发送消息:

./msgtool s 1 "Hello"


一个终端接受消息:

./msgtool r 0


这时将收到消息:Hello。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值