进程间通信系列 之 消息队列应用实例

 进程间通信系列 之 概述与对比   http://blog.csdn.net/younger_china/article/details/15808685
 进程间通信系列 之 共享内存及其实例   http://blog.csdn.net/younger_china/article/details/15961557
 进程间通信系列 之 共享内存简单实例   http://blog.csdn.net/younger_china/article/details/15991081
 进程间通信系列 之 信号(理论)   http://blog.csdn.net/younger_china/article/details/15976961
 进程间通信系列 之 信号实例   http://blog.csdn.net/younger_china/article/details/15968715
 进程间通信系列 之 信号综合实例   http://blog.csdn.net/younger_china/article/details/15980485
 进程间通信系列 之 命名管道FIFO及其应用实例   http://blog.csdn.net/younger_china/article/details/15808531
 进程间通信系列 之 管道(客户端和服务端通信)   http://blog.csdn.net/younger_china/article/details/15809281
 进程间通信系列 之 信号量详解及编程实例   http://blog.csdn.net/younger_china/article/details/15808531
 进程间通信系列 之 消息队列函数及其范例   http://blog.csdn.net/younger_china/article/details/15503871
 进程间通信系列 之 消息队列应用实例   http://blog.csdn.net/younger_china/article/details/15808501 
 进程间通信系列 之 socket套接字及其实例   http://blog.csdn.net/younger_china/article/details/15809163
 进程间通信系列 之 socket套接字实例   http://blog.csdn.net/younger_china/article/details/15809207


场景:

    进程A向消息队列写消息,而进程B则从消息队列读消息。

消息队列和之前的管道不同,消息队列是相对独立于进程的,它不需要进程自己来提供同步方法。消息队列里面有消息就可以读。

读取消息实例:msg_rcv.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct	my_msg_st {
	long int my_msg_type;
	char some_text[BUFSIZ];
};

int main(int argc,char **argv)
{
	int	running = 1;
	int	msgid;
	struct	my_msg_st some_data;
	long int msg_to_receive = 0;

	msgid = msgget((key_t)1234,0666 | IPC_CREAT);
	if(msgid == -1){
		fprintf(stderr,"Msgget failed!\n");
		exit(-1);
	}

	while(running){
		if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,0) == -1){
			fprintf(stderr,"Msgrcv failed!\n");
			exit(-1);
		}
		printf("You wrote: %s",some_data.some_text);
		if(strncmp(some_data.some_text,"end",3) == 0){
			running = 0;
		}
	}

	if(msgctl(msgid,IPC_RMID,0) == -1){
		fprintf(stderr,"Msgctl failed!\n");
		exit(-1);
	}

	exit(0);
}

写入消息队列应用:msg_snd.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define	MAX_TEXT	256

struct	my_msg_st {
	long int my_msg_type;
	char some_text[BUFSIZ];
};

int main(int argc,char **argv)
{
	int	running = 1;
	int	msgid;
	struct	my_msg_st some_data;
	char	buffer[BUFSIZ];

	msgid = msgget((key_t)1234,0666 | IPC_CREAT);
	if(msgid == -1){
		fprintf(stderr,"Msgget failed!\n");
		exit(-1);
	}

	while(running){
		printf("Enter Some Text: ");
		fgets(buffer,BUFSIZ,stdin);
		some_data.my_msg_type = 1;
		strcpy(some_data.some_text,buffer);
		if(msgsnd(msgid,(void *)&some_data,MAX_TEXT,0) == -1){
			fprintf(stderr,"Msgsnd failed!\n");
			exit(-1);
		}
		if(strncmp(some_data.some_text,"end",3) == 0){
			running = 0;
		}
	}
	exit(0);
}


 

注意,这个程序的验证并不需要两个进程同时运行。

首先运行snd进程,写入几条消息。

然后启动rcv进程,读取消息并显示,最后删除消息队列文件。

备注:查看系统进程的IPC资源命令

# ipcs      //查看ipc资源

# ipcrm   //删除ipc资源

 

转自http://blog.csdn.net/nowdoit/article/details/7095464

 



posted on 2013-11-13 08:33  YoungerChina  阅读(332)  评论(0编辑  收藏  举报

导航