Linux_父子进程通过消息队列通信
发布于2018-04-27 11:40:11
-
#include
<stdio.h>
-
#include
<wait.h>
-
#include
<stdlib.h>
-
#include
<string.h>
-
#include
<sys/types.h>
-
#include
<sys/ipc.h>
-
#include
<sys/msg.h>
-
#include
<unistd.h>
-
-
#define SIZE 1024
-
-
const long id = 1000;//这里规定类型为1000的消息
-
-
typedef struct msgqueue{
-
long mytype;//消息类型
-
char mytext[SIZE];//消息内容
-
}msgqueue;
-
-
int main(){
-
key_t k = ftok(".",0x1234);//申请key,确保两个需要通信的进程使用同一个key值获取到的队列id
-
if(k
< 0){
-
perror("ftok");
-
return -1;
-
}
-
int msgid = msgget(k,IPC_CREAT|IPC_EXCL|0666);//使用key值获取id
-
if(msgid < 0){
-
perror("msgget");
-
return -1;
-
}
-
pid_t pid = fork();
-
if(pid < 0){
-
perror("fork");
-
return -1;
-
}
-
if(pid > 0){//父进程发消息
-
msgqueue queue;
-
queue.mytype = id;//发送的消息类型为1000
-
char msg[SIZE];
-
while(1){//循环输入,此时输入的每一条消息,消息类型都是1000
-
printf("father says# ");
-
// fflush(stdout);
-
fgets(msg,sizeof(msg)+1,stdin);//从标准输入获取字符串,以回车换行结束
-
strcpy(queue.mytext,msg);
-
if(msgsnd(msgid,&queue,sizeof(msg),0) == -1){//将信息挂载到消息队列里
-
perror("msgsnd");
-
return -1;
-
}
-
if(strncmp(msg,"end",3) == 0){//只要输入end,认为结束通信
-
break;
-
}
-
usleep(500000);
-
}
-
waitpid(pid,NULL,0);//等待子进程退出
-
}else if(pid == 0){//子进程取消息,注意消息队列里只要有人拿了消息,那么拿走的消息就不在消息队列里了
-
msgqueue queue;
-
while(1){//循环拿消息
-
if(msgrcv(msgid,&queue,sizeof(queue.mytext),id,0) == -1){//从消息队列里取消息,取一个少一个
-
perror("msgrcv");
-
return -1;
-
}
-
if(strncmp(queue.mytext,"end",3)==0){//end就退出
-
printf("father will quit,i quit too\n");
-
break;
-
}
-
printf("child recv:%s\n",queue.mytext);
-
}
-
exit(EXIT_SUCCESS);
-
}
-
if(msgctl(msgid,IPC_RMID,NULL)
<0){//删除消息队列,因为消息队列创建后不会自己删除,必须人为删除或者重启
-
perror("msgctl");
-
return -1;
-
}
-
printf("\n done \n");//没什么含义,只是为了表示这次的传输是正常退出的
-
return 0;
-
}
-
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count"></span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true"> <use xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/qq_40425540"> <img src="https://profile.csdnimg.cn/2/A/2/3_qq_40425540" class="avatar_pic" username="qq_40425540"> <img src="https://g.csdnimg.cn/static/user-reg-year/2x/3.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/qq_40425540" data-report-click="{"mod":"popu_379"}" target="_blank">s1f</a></span> </div> <div class="text"><span>发布了29 篇原创文章</span> · <span>获赞 7</span> · <span>访问量 8463</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=qq_40425540" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div> </article>