进程A代码:
#include <stdio.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <stdlib.h>#include <string.h>#define MSG_EXCEPT 020000struct msgbuf{long mtype;char mtext[100];};int main(int argc, const char *argv[]){//key是msgget中的键值//ftok中的第二个参数是一个非零数key_t key1 = ftok("/home/ubuntu/",2);if(key1<0){perror("ftok");return-1;}printf("key = %#x\n",key1);//创建消息队列int msqid1 = msgget(key1,IPC_CREAT|0664);if(msqid1<0){perror("msgget");return -1;}printf("msqid = %d\n",msqid1);key_t key2 = ftok("/home/ubuntu/",2);if(key2<0){perror("ftok");return-1;}printf("key = %#x\n",key2);//创建消息队列int msqid2 = msgget(key2,IPC_CREAT|0664);if(msqid2<0){perror("msgget");return -1;}printf("msqid = %d\n",msqid2);struct msgbuf snd;//A发送while(1){printf("请输入消息类型>>>");scanf("%ld",&snd.mtype);getchar();if(snd.mtype <= 0){break;}printf("请输入消息内容>>");fgets(snd.mtext,sizeof(snd.mtext),stdin);snd.mtext[strlen(snd.mtext)-1] = '\0';//第一个参数是消息队列,第二个参数消息队列的类型和内容//第三个参数消息内容的大小,第四个参数0:阻塞方式发送if(msgsnd(msqid1,(void*)&snd,sizeof(snd.mtext),0)<0){perror("msgsnd");return -1;}if(strcmp(snd.mtext,"quit") == 0){break;}printf("发送消息成功\n"); system("ipcs -q");ssize_t res = 0;res = msgrcv(msqid2,&snd,sizeof(snd.mtext),0,0);if(res < 0){perror("msgrcv");return -1;}if(strcmp(snd.mtext,"quit") == 0){break;}printf("res=%ld | mtype=%ld:mtext=%s\n",res,snd.mtype,snd.mtext);}if(msgctl(msqid1,IPC_RMID,NULL) < 0){perror("msgctl");return -1;}if(msgctl(msqid2,IPC_RMID,NULL) < 0){perror("msgctl");return -1;}printf("删除队列成功!\n");system("ipcs -q");return 0;}
进程B代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
//MSG_EXCEPT的宏需要追加复制过来
#define MSG_EXCEPT 020000
struct msgbuf
{long mtype;char mtext[100];
};
int main(int argc, const char *argv[])
{key_t key1 = ftok("/home/ubuntu/",2);if(key1<0){perror("ftok");return-1;}printf("key1 = %#x\n",key1);int msqid1 = msgget(key1,IPC_CREAT|0664);if(msqid1<0){perror("msgget");return -1;}printf("msqid = %d\n",msqid1);key_t key2 = ftok("/home/ubuntu/",2);if(key2<0){perror("ftok");return-1;}printf("key = %#x\n",key2);int msqid2 = msgget(key2,IPC_CREAT|0664);if(msqid2<0){perror("msgget");return -1;}printf("msqid = %d\n",msqid2);ssize_t res = 0;struct msgbuf rcv;while(1){system("ipcs -q");//A接收//第三个参数,msgtyp==0,读取队列中第一条消息,先进先出//>0指定消息类型读取//第四个参数,0为阻塞方式接收,IPC_NOWAIT非阻塞方式接收//读取消息队列中的第一条消息res = msgrcv(msqid1,&rcv,sizeof(rcv.mtext),0,0);/* //读取消息队列中的第一条消息类型为100的消息res = msgrcv(msqid,&rcv,sizeof(rcv.mtext),100,0);//读取消息队列中的第一条消息类型不为100的消息res = msgrcv(msqid,&rcv,sizeof(rcv.mtext),100,MSG_EXCEPT);*/if(strcmp(rcv.mtext,"quit") == 0){break;}if(res < 0){perror("msgrcv");return -1;}printf("res=%ld | mtype=%ld:mtext=%s\n",res,rcv.mtype,rcv.mtext);//B发送printf("请输入消息类型>>>");scanf("%ld",&rcv.mtype);getchar();printf("请输入消息内容>>>");fgets(rcv.mtext,sizeof(rcv.mtext),stdin);if(strcmp(rcv.mtext,"quit") == 0){break;}rcv.mtext[strlen(rcv.mtext)-1] = '\0';if(msgsnd(msqid2,(void*)&rcv,sizeof(rcv.mtext),0)<0){perror("msgsnd");return -1;}printf("发送消息成功!\n");system("ipcs -q");}//删除消息队列if(msgctl(msqid1,IPC_RMID,NULL) < 0){perror("msgctl");return-1;}if(msgctl(msqid2,IPC_RMID,NULL) < 0){perror("msgctl");return-1;}printf("删除消息队列成功!\n");system("ipcs -q");return 0;
}