现在的位置: 首页 > 综合 > 正文

Linux 进程间通信——消息队列实现双向通信

2013年10月24日 ⁄ 综合 ⁄ 共 3038字 ⁄ 字号 评论关闭

函数: key_t ftok(const char *filename, int proj_id);
通过文件名和项目号获得System V IPC键值(用于创建消息队列、共享内存所用)
proj_id:项目号,不为0即可
返回:成功则返回键值,失败则返回-1

函数: int msgget(key_t key, int msgflg);
key:键值,当为IPC_PRIVATE时新建一块共享内存;
shmflg:标志。
IPC_CREAT:内存不存在则新建,否则打开;
IPC_EXCL:只有在内存不存在时才创建,否则出错。
返回:成功则返回标识符,出错返回-1

函数: int msgsnd(int msgid, const void *msgp, size_t sz, int flg);
向消息队列发送消息
msgid:通过msgget获取
msgp:指向消息内容的指针
sz:消息内容的大小
flg:处理方式;如为IPC_NOWAIT时表示空间不足时不会阻塞
返回:成功则返回0,失败返回-1

函数: int msgrcv(int msgid, void *msgp, size_t sz, long type, int flg);
从消息队列读取消息
msgid:通过msgget获取
msgp:指向消息内容的指针
sz:消息内容的大小
type:指定接收的消息类型;若为0则队列第一条消息将被读取,而不管类型;若大于0则队列中同类型的消息将被读取,如在flg中设了MSG_RXCEPT位将读取指定类型的其他消息;若小于0读取绝对值小于type的消息。
flg:处理方式;
返回:成功返回收到消息长度,错误返回-1

函数: int msgctl(int msgid, int cmd, struct msgid_ds *buf);
msgid:通过msgget获取
cmd:控制命令,如下:
IPC_STAT:获取消息队列状态
IPC_SET:改变消息队列状态
IPC_RMID:删除消息队列
buf:结构体指针,用于存放消息队列状态

返回:成功返回与cmd相关的正数,错误返回-1

Client.c

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

#define N 1024
#define Pathname "/tmp/xkeyideal"
#define MODE IPC_CREAT|IPC_EXCL|0666
#define ID 27
#define SENDMSG 1
#define RECVMSG 2

struct msgbuf{
	int type;
	int a;
	int b;
	char text[N];
}msg_rbuf,msg_sbuf;

int main(){
	key_t key;
	key = ftok(Pathname,ID);
	int msgid;
	msgid = msgget(key,MODE);
	if(msgid == -1){
		printf("error");
		exit(1);
	}
	while(1){
		pid_t pid,pid_wait;
		pid = fork();
		if(pid > 0){
			pid_wait = waitpid(pid,NULL,0);//父进程等待子进程先执行
			printf("Parent process recv msg,pid = %d\n",getpid());
			msgrcv(msgid,&msg_rbuf,sizeof(msg_rbuf),SENDMSG,0);
			int a = msg_rbuf.a;
			int b = msg_rbuf.b;
			printf("color: Receive: %s, sum %d + %d = %d\n",msg_rbuf.text,a,b,a+b);
		}else if(pid == 0){
			char str[N];
			printf("Child process send msg, pid = %d\n",getpid());
			printf("Please input msg info str ,a ,b\n");
			scanf("%s %d %d",str,&msg_sbuf.a,&msg_sbuf.b);
			strcpy(msg_sbuf.text,str);
			msg_sbuf.type = RECVMSG;
			msgsnd(msgid,&msg_sbuf,sizeof(msg_sbuf),IPC_NOWAIT);
			exit(0);
		}
	}
	msgctl(msgid,IPC_RMID,NULL);
	exit(0);
}

Server.c

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#define N 1024
#define Pathname "/tmp/xkeyideal"
#define MODE IPC_CREAT|IPC_EXCL|0666
#define ID 27
#define SENDMSG 1
#define RECVMSG 2

struct msgbuf{
	int type;
	int a;
	int b;
	char text[N];
}msg_rbuf;
struct msgbuf2{
	int type;
	int a;
	int b;
	char text[N];
}msg_sbuf;

int main(){
	key_t key;
	key = ftok(Pathname,ID);
	int msgid;
	msgid = msgget(key,0);//这里和上面的有不同
	if(msgid == -1){
		perror("Msgqueue has exist");
		exit(1);
	}
	while(1){
		pid_t pid;
		pid = fork();
		if(pid > 0){//parent
			wait(NULL);
			msg_sbuf.type = SENDMSG;
			char str[N];
			printf("Please input info: name , a, b\n");
			scanf("%s %d %d",str,&msg_sbuf.a,&msg_sbuf.b);
			strcpy(msg_sbuf.text,str);
			printf("Parent process send msg, pid = %d\n",getpid());
			msgsnd(msgid, &msg_sbuf,sizeof(msg_sbuf),IPC_NOWAIT);
		}else if(pid == 0){
			msgrcv(msgid,&msg_rbuf,sizeof(msg_rbuf),RECVMSG,0);
			printf("Child process recv msg, pid = %d\n",getpid());
			printf("xkey: Receive: %s, sum %d + %d = %d \n",msg_rbuf.text,msg_rbuf.a,msg_rbuf.b,msg_rbuf.a+msg_rbuf.b);
			exit(1);
		}
	}
	msgctl(msgid,IPC_RMID,NULL);
	exit(0);
}

先启动Client再启动Server

另:在终端输入ipcs -q能看到创建的消息队列,ipcrm -q <msgqid>能够手动删除该消息队列

抱歉!评论已关闭.