经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
c/c++ linux 进程间通信系列6,使用消息队列(message queue)
来源:cnblogs  作者:小石王  时间:2018/10/21 20:27:09  对本文有异议

linux 进程间通信系列6,使用消息队列(message queue)

概念:消息排队,先进先出(FIFO),消息一旦出队,就从队列里消失了。

1,创建消息队列(message queue)

2,写消息到消息队列(message queue)

3,从消息队列(message queue)读消息

3,删除消息队列(message queue)

1,创建消息队列(message queue)

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/ipc.h>
  4. #include <sys/msg.h>
  5. int main(){
  6. int msgid;
  7. msgid = msgget(IPC_PRIVATE, 0600);
  8. if(msgid < 0){
  9. perror("msgget");
  10. return 1;
  11. }
  12. printf("%d\n", msgid);
  13. return 0;
  14. }

github源代码

用下面的命令,能够查看到上面的程序创建的共享内存。

  1. ipcs -q

执行后的结果:

  1. ------ Message Queues --------
  2. key msqid owner perms used-bytes messages
  3. 0x00000000 32768 ys 600 0 0

2,写消息到消息队列(message queue)

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/ipc.h>
  4. #include <sys/msg.h>
  5. #include <stdlib.h>
  6. #define MTEXTSIZE 10
  7. int main(int argc, char* argv[]){
  8. int msgid;
  9. struct msgbuf{
  10. long mtype;
  11. char mtext[MTEXTSIZE];
  12. }mbuf;
  13. if(argc != 2){
  14. printf("wrong argc");
  15. return 1;
  16. }
  17. msgid = atoi(argv[1]);
  18. mbuf.mtype = 777;
  19. memset(mbuf.mtext, 0, sizeof(mbuf.mtext));
  20. mbuf.mtext[0] = 'A';
  21. if(msgsnd(msgid, &mbuf, MTEXTSIZE, 0) != 0){
  22. perror("msgsnd");
  23. return 1;
  24. }
  25. return 0;
  26. }

github源代码

执行方法:【ipcs -q】执行后,得到下面的数字。

  1. ./a.out 789884

执行后:ipcs -q 发现, message下面的数字从0变为1了,说明消息队列里有了一个消息。

  1. ------ Message Queues --------
  2. key msqid owner perms used-bytes messages
  3. 0x00000000 32768 ys 600 10 1

3,从消息队列(message queue)读消息

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/ipc.h>
  4. #include <sys/msg.h>
  5. #include <stdlib.h>
  6. #define MTEXTSIZE 10
  7. int main(int argc, char* argv[]){
  8. int msgid, msgtype;
  9. struct msgbuf{
  10. long mtype;
  11. char mtext[MTEXTSIZE];
  12. }mbuf;
  13. if(argc != 3){
  14. printf("wrong argc");
  15. return 1;
  16. }
  17. msgid = atoi(argv[1]);
  18. msgtype = atoi(argv[2]);
  19. if(msgrcv(msgid, &mbuf, MTEXTSIZE, msgtype, 0) <= 0){
  20. perror("msgrcv");
  21. return 1;
  22. }
  23. printf("%c\n", mbuf.mtext[0]);
  24. return 0;
  25. }

github源代码

执行方法:必须指定在写入消息是的type,也就是777

  1. ./a.out 32768 777

执行后,ipcs -q发现,message下面的数字,由1变为0了,说明消息队列里没有消息了。

  1. ------ Message Queues --------
  2. key msqid owner perms used-bytes messages
  3. 0x00000000 32768 ys 600 0 0

4,删除消息队列(message queue)

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/ipc.h>
  4. #include <sys/msg.h>
  5. #include <stdlib.h>
  6. int main(int argc, char* argv[]){
  7. int msgid;
  8. msqid_ds mds;
  9. if(argc != 2){
  10. printf("wrong argv\n");
  11. return 1;
  12. }
  13. msgid = atoi(argv[1]);
  14. if(msgctl(msgid, IPC_RMID, &mds) != 0){
  15. perror("msgctl");
  16. return 1;
  17. }
  18. return 0;
  19. }

执行方法:

  1. ./a.out 32768

执行后,ipcs -q发现,消息队列本身都没有了。

  1. ------ Message Queues --------
  2. key msqid owner perms used-bytes messages

用命令行删除共享内存:【ipcs -q】执行后,得到下面的数字。

  1. ipcrm -q id

github源代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号