码迷,mamicode.com
首页 > 编程语言 > 详细

C语言消息队列

时间:2017-10-14 17:11:27      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:struct   技术   .com   ipcs   har   pcs   color   png   sgid   

msgsnd.c文件

 1 #include<sys/types.h>
 2 #include<sys/ipc.h>
 3 #include<sys/msg.h>
 4 #include<stdio.h>
 5 
 6 struct msgbuf
 7 {
 8     long type;//类型          
 9     char buf[1024];
10 };
11 
12 int main()
13 {
14     int msgid;
15     msgid=msgget(0x1000,IPC_CREAT | 0777);
16     struct msgbuf mb={1,"hello world"};
17     int ret;
18     ret=msgsnd(msgid,&mb,sizeof(struct msgbuf)-sizeof(long),0);
19     //这里的长度不包括类型的大小
20 }

msgrcv.c文件

 1 #include<sys/types.h>
 2 #include<sys/ipc.h>
 3 #include<sys/msg.h>
 4 #include<stdio.h>
 5 struct msgbuf
 6 {
 7     long type;
 8     char buf[1024];
 9 };
10 int main()
11 {
12     int msgid;
13     msgid=msgget(0x1000,IPC_CREAT | 0777);
14     struct msgbuf mb;
15     msgrcv(msgid,&mb,sizeof(struct msgbuf)-sizeof(long),1,0);
16     puts(mb.buf);
17 }

gcc msgsnd.c -o msgsnd      gcc msgrcv.c -o msgrcv 

ipcs查看消息队列

技术分享

./msgsnd   然后 ipcs

技术分享

已经传入,然后./msgrcv

技术分享

ipcs查看一下

技术分享

取出数据

C语言消息队列

标签:struct   技术   .com   ipcs   har   pcs   color   png   sgid   

原文地址:http://www.cnblogs.com/ysjd/p/7667334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!