Linux C 进程间的IPC通信 之 消息队列(1)

来源:互联网 发布:淘宝公主裙 编辑:程序博客网 时间:2024/05/17 04:55

1、消息队列 与 共享内存

1-1 消息队列  和 管道 都属于 队列结构,读完了 就被删除 不存在,但 管道 是 顺序队列 ,而 消息队列 是 链式队列

        1-2  消息队列 的读和写 可设为 阻塞模式。一个进程 写完后,无需 像共享内存般 每次都要 通知对方 并睡眠等待

2、参数为 宏 IPC_PRIVATE

         int msgid;      msgid = msgget(IPC_PRIVATE, IPC_CREAT | 0777);//创建 消息队列

                                 fgets(sendbuf, 120, stdin);//从键盘输入到缓存变量

                                 msgsnd(msgid, &sendbuf, strlen(sendbuf.content, 0));//写入消息队列 【写到哪里去、写什么、写多少个、参数0为阻塞模式】

         int  recvLen;     msgrcv(msgid, &recvbuf, 120, 333, 0);//读取消息队列【从哪里读、读到哪里去、读多少个、long类型的type、参数0为阻塞模式】

                                 msgctl(msgid, IPC_RMID, NULL);//删除消息队列,等同于 命令“ipcrm -q <id>”

                                 system("ipcs -q")

3、参数为 ftok() 返回值

        int key;            key = ftok("./a", 'b');

        int msgid;      msgid = msgget(key, IPC_CREAT | 0777);


代码 1 (初见):

  1 #include <stdio.h>  2 #include <sys/msg.h>  3 #include <string.h>  4   5 struct msgbuf  6 {  7         long type;      //注意注意:此处必须为 long型,否则 编译能通过,但 执行出错。。。  8         char content[120];  9         char ID[4]; 10 }; 11  12 int main() 13 { 14         int msgid; 15         struct msgbuf sendbuf, recvbuf; 16         int recvLen; 17  18         msgid = msgget(IPC_PRIVATE, 0777);      //创建 消息队列 19         if(msgid < 0) 20         { 21                 printf("message queue create failure.\n"); 22                 return -1; 23         } 24         printf("message queue msgid = %d\n",msgid); 25         system("ipcs -q"); 26  27         sendbuf.type = 333;     //类型标识 28         printf("please input :\n"); 29         fgets(sendbuf.content, 120, stdin);     //从键盘输入到 sendbuf.content 变量中 30  31         //发给谁、发什么、发多少、参数0:以阻塞方式发送 32         msgsnd(msgid, &sendbuf, strlen(sendbuf.content), 0);    //写入 消息队列 33  34         recvLen = msgrcv(msgid, &recvbuf, 120, 333, 0); //读取 消息队列 35  36         printf("receive message:[%d bytes] %s", recvLen-1, recvbuf.content); 37  38         msgctl(msgid, IPC_RMID, NULL);  //删除 消息队列 39         system("ipcs -q"); 40         return 0; 41 }
执行 1(初见):



代码 2(相识)之(无亲缘关系)进程间的单向通信

【则一】

  1 #include <stdio.h>  2 #include <string.h>  3 #include <sys/msg.h>  4   5 struct msgbuf   //结构体  6 {  7         long type;      //类型标识  8         char content[120];  9         char flag[4]; 10 }; 11  12 int main() 13 { 14         int key; 15         int msgid; 16         struct msgbuf sendbuf; 17  18         key = ftok("./a",'b');  //创建 key值 19         msgid = msgget(key, IPC_CREAT | 0777);  //创建消息队列 20  21         sendbuf.type = 333;     //设置类型标识 22         while(1) 23         { 24                 printf("please input: "); 25                 fgets(sendbuf.content, 120, stdin);     //从键盘输入到缓存 26  27                 msgsnd(msgid, &sendbuf, strlen(sendbuf.content), 0);    //写入 消息队列 28         } 29         return 0; 30 }

【则二】

  1 #include <stdio.h>  2 #include <string.h>  3 #include <sys/msg.h>  4   5 struct msgbuf  6 {  7         long type;  8         char content[116];  9         char flag[4]; 10 }; 11  12 int main() 13 { 14         int key; 15         int msgid; 16         struct msgbuf recvbuf; 17         int recvLen; 18  19         key = ftok("./a",'b');  //创建 key值 20         if(key < 0) 21         { 22                 printf("key create failure.\n"); 23                 return -1; 24         } 25  26         msgid = msgget(key, IPC_CREAT | 0777);  //创建 消息队列 27         if(msgid < 0) 28         { 29                 printf("message queue create failure.\n"); 30                 return -2; 31         } 32         while(1) 33         { 34                 recvLen = msgrcv(msgid, &recvbuf, 116, 333, 0); //读取 消息队列 35                 printf("receive message[%d bytes]: %s\n", recvLen, recvbuf.content); 36         } 37         return 0; 38  39 }
执行 2 (相识):




阅读全文
0 0
原创粉丝点击