1、消息队列代码
msgsnd.c
#include<myhead.h>
//定义一个消息类型
struct msgbuf
{long mtype; //消息类型char mtext[1024]; //消息正文
};//定义一个宏,表示消息正文大小
#define MSGSIZE sizeof(struct msgbuf)-sizeof(long)int main(int argc, const char *argv[])
{//定义一个key值key_t key = 0;if((key=ftok("/",'k')) == -1){perror("ftok error");return -1;}printf("ftok success key=%#x\n",key);//根据key值创建一个消息队列int msqid = -1;if((msqid=msgget(key,IPC_CREAT|0664)) == -1){perror("msgget error");return -1;}printf("msgget success msqid=%d\n",msqid);//定义一个消息struct msgbuf sbuf;while(1){//清空正文的容器bzero(sbuf.mtext,sizeof(sbuf.mtext));//输入消息类型 printf("please enter the type of message:");scanf("%ld",&sbuf.mtype);getchar();//输入消息的内容printf("please enter the text of message:");fgets(sbuf.mtext,sizeof(sbuf.mtext),stdin);sbuf.mtext[strlen(sbuf.mtext)-1] = 0;//将消息存放到队列中msgsnd(msqid,&sbuf,MSGSIZE,0);printf("send success\n");if(strcmp(sbuf.mtext,"quit")==0)break;}return 0;
}
msgrcv.c
#include<myhead.h>
//定义一个消息类型
struct msgbuf
{long mtype; //消息类型char mtext[1024]; //消息正文
};
//定义一个宏,表示消息正文大小
#define MSGSIZE sizeof(struct msgbuf)-sizeof(long)int main(int argc, const char *argv[])
{//1、创建key值key_t key = 0;if((key=ftok("/",'k'))== -1 ){perror("ftok error");return -1;}//2、根据key值创建一个消息队列int msqid = -1;if((msqid=msgget(key,IPC_CREAT|0664)) == -1){perror("msgget error");return -1;}printf("msgget success msqid=%d\n",msqid);//定义一个消息struct msgbuf rbuf;while(1){//清空容器bzero(rbuf.mtext,sizeof(rbuf.mtext));//从消息列表中读取一个消息msgrcv(msqid,&rbuf,MSGSIZE,0,0);printf("the massage receive:%s\n",rbuf.mtext);if(strcmp(rbuf.mtext,"quit")==0)break;}//将消息队列删除if(msgctl(msqid,IPC_RMID,NULL) != 0){perror("msgclt error");return -1;}return 0;
}
2、使用消息队列完成两个进程间相互通信
send.c
#include<myhead.h>
//定义一个消息
struct msgbuf
{long mtype; //消息类型char mtext[1024]; //消息正文内容
};//定义一个宏,表示消息正文大小
#define MSGSIZE sizeof(struct msgbuf)-sizeof(long)int main(int argc, const char *argv[])
{//定义一个key值key_t key=0;if((key=ftok("/",'k')) == -1){perror("ftok error");return -1;}printf("ftok success key=%#x\n",key);//根据Key值创建一个消息队列int msqid = -1;if((msqid=msgget(key,IPC_CREAT|0664)) == -1){perror("msgget error");return -1;}//定义一个消息struct msgbuf sbuf;struct msgbuf rbuf;//创建一个进程pid_t pid=fork();if(pid > 0){//父进程while(1){//清空正文的容器bzero(sbuf.mtext,sizeof(sbuf.mtext));//输入消息类型printf("please enter the type of message:");scanf("%ld",&sbuf.mtype);getchar();//输入消息的内容printf("请输入消息的内容:");fgets(sbuf.mtext,sizeof(sbuf.mtext),stdin);sbuf.mtext[strlen(sbuf.mtext)-1] = 0;//将消息存放到队列中msgsnd(msqid,&sbuf,MSGSIZE,0);printf("send success\n");if(strcmp(sbuf.mtext,"quit") == 0)break;}wait(NULL);}else if(pid == 0){while(1){//清空容器bzero(rbuf.mtext, sizeof(rbuf.mtext));//从消息队列中读取一个消息//msgrcv(msqid, &rbuf, MSGSIZE, 0, 0);//第一个0:表示一直读取队列中第一个消息//第二个0:表示阻塞读取msgrcv(msqid, &rbuf, MSGSIZE, 2, 0); //只接收类型为1printf("\n收到消息为:%s\n", rbuf.mtext);if(strcmp(rbuf.mtext, "quit") == 0);break;}exit(EXIT_SUCCESS); //退出进程}else{perror("fork error");return -1;}return 0;
}recevie.c
#include<myhead.h>
//定义一个消息
struct msgbuf
{long mtype; //消息类型char mtext[1024]; //消息正文内容
};//定义一个宏,表示消息正文大小
#define MSGSIZE sizeof(struct msgbuf)-sizeof(long)int main(int argc, const char *argv[])
{//定义一个key值key_t key=0;if((key=ftok("/",'k')) == -1){perror("ftok error");return -1;}printf("ftok success key=%#x\n",key);//根据Key值创建一个消息队列int msqid = -1;if((msqid=msgget(key,IPC_CREAT|0664)) == -1){perror("msgget error");return -1;}//定义一个消息struct msgbuf sbuf;struct msgbuf rbuf;//创建一个进程pid_t pid=fork();if(pid > 0){//父进程while(1){//清空正文的容器bzero(sbuf.mtext,sizeof(sbuf.mtext));//输入消息类型printf("please enter the type of message:");scanf("%ld",&sbuf.mtype);getchar();//输入消息的内容printf("请输入消息的内容:");fgets(sbuf.mtext,sizeof(sbuf.mtext),stdin);sbuf.mtext[strlen(sbuf.mtext)-1] = 0;//将消息存放到队列中msgsnd(msqid,&sbuf,MSGSIZE,0);printf("send success\n");if(strcmp(sbuf.mtext,"quit") == 0)break;}wait(NULL);}else if(pid == 0){while(1){//清空容器bzero(rbuf.mtext, sizeof(rbuf.mtext));//从消息队列中读取一个消息//msgrcv(msqid, &rbuf, MSGSIZE, 0, 0);//第一个0:表示一直读取队列中第一个消息//第二个0:表示阻塞读取msgrcv(msqid, &rbuf, MSGSIZE, 1, 0); //只接收类型为1printf("\n收到消息为:%s\n", rbuf.mtext);if(strcmp(rbuf.mtext, "quit") == 0);break;}exit(EXIT_SUCCESS); //退出进程}else{perror("fork error");return -1;}return 0;
}