Linux进程间通信——消息队列

简介:

2000元阿里云代金券免费领取,2核4G云服务器仅664元/3年,新老用户都有优惠,立即抢购>>>


阿里云采购季(云主机223元/3年)活动入口:请点击进入>>>,


阿里云学生服务器(9.5元/月)购买入口:请点击进入>>>,

消息队列是消息的链接表,包括Posix消息队列system V消息队列。消息队列用于运行于同一台机器上的进程间通信,它
和管道很相似,有足够权限的进程可以向队列中添加消息,被赋予读权限的进程则可以读走队列中的消息。消息队列克服了
信号承载信息量少,管道只能承载无格式字节流以及缓冲区大小受限等缺点。 我们可以用流管道或者套接口的方式来取代它。

查询系统消息队列:ipcs -q

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

int msgctl(int msqid, int cmd, struct msqid_ds *buf);

int msgget(key_t key, int msgflg);

int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg);

int msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg);


C代码
  1. /*msgserver.c*/ 
  2.  
  3. #include <stdlib.h> 
  4. #include <string.h> 
  5. #include <errno.h> 
  6. #include <sys/types.h> 
  7. #include <sys/ipc.h> 
  8. #include <sys/msg.h> 
  9. #include <sys/stat.h> 
  10.  
  11. #define   MSG_FILE "msgserver.c"  
  12. #define   BUFFER 255  
  13. #define   PERM S_IRUSR|S_IWUSR  
  14. /* 服务端创建的消息队列最后没有删除,我们要使用ipcrm命令来删除的 */ 
  15. /* ipcrm -q <msqid> */ 
  16.  
  17. struct msgtype  
  18. {  
  19.     long mtype;  
  20.     char buffer[BUFFER+1];  
  21. };  
  22.  
  23. int main()  
  24. {  
  25.     struct msgtype msg;  
  26.     key_t key;  
  27.     int msgid;  
  28.      
  29.     if((key=ftok(MSG_FILE,'a'))==-1)  
  30.     {  
  31.         fprintf(stderr,"Creat Key Error:%s\n", strerror(errno));  
  32.         exit(1);  
  33.     }  
  34.  
  35.     if((msgid=msgget(key, PERM|IPC_CREAT|IPC_EXCL))==-1)  
  36.     { 
  37.         fprintf(stderr, "Creat Message Error:%s\n", strerror(errno));  
  38.         exit(1); 
  39.     }  
  40.     printf("msqid = %d\n", msgid); 
  41.     while(1) 
  42.     {  
  43.         msgrcv(msgid, &msg, sizeof(struct msgtype), 1, 0);  
  44.         fprintf(stderr,"Server Receive:%s\n", msg.buffer);  
  45.         msg.mtype = 2;  
  46.         msgsnd(msgid, &msg, sizeof(struct msgtype), 0);  
  47.     }  
  48.     exit(0);  
  49. }    
C代码
  1. /* msgclient.c */ 
  2.  
  3. #include <stdio.h> 
  4. #include <stdlib.h> 
  5. #include <string.h> 
  6. #include <errno.h> 
  7. #include <sys/types.h> 
  8. #include <sys/ipc.h> 
  9. #include <sys/msg.h> 
  10. #include <sys/stat.h> 
  11.  
  12. #define   MSG_FILE "msgserver.c"  
  13. #define   BUFFER 255  
  14. #define   PERM S_IRUSR|S_IWUSR  
  15.  
  16. struct msgtype {  
  17.     long mtype;  
  18.     char buffer[BUFFER+1];  
  19. };  
  20.  
  21. int main(int argc, char **argv)  
  22. {  
  23.     struct msgtype msg;  
  24.     key_t key;  
  25.     int msgid;  
  26.      
  27.     if(argc != 2)  
  28.     {  
  29.         fprintf(stderr,"Usage:%s string\n", argv[0]);  
  30.         exit(1);  
  31.     }  
  32.      
  33.     if((key=ftok(MSG_FILE,'a'))==-1)  
  34.     {  
  35.         fprintf(stderr,"Creat Key Error:%s\n", strerror(errno));  
  36.         exit(1);  
  37.     }  
  38.      
  39.     if((msgid=msgget(key, PERM))==-1)  
  40.     {  
  41.         fprintf(stderr,"Creat Message  Error:%s\n", strerror(errno));  
  42.         exit(1);  
  43.     }  
  44.      
  45.     msg.mtype = 1;  
  46.     strncpy(msg.buffer, argv[1], BUFFER);  
  47.     msgsnd(msgid, &msg, sizeof(struct msgtype), 0);   
  48.     memset(&msg, '\0'sizeof(struct msgtype));  
  49.     msgrcv(msgid, &msg, sizeof(struct msgtype), 2, 0);  
  50.     fprintf(stderr, "Client receive:%s\n", msg.buffer);  
  51.     exit(0); 
  52. }   
本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2010/03/24/1693485.html,如需转载请自行联系原作者

相关文章
|
1天前
|
算法 Linux 调度
深度解析:Linux内核的进程调度机制
【5月更文挑战第29天】 在现代操作系统中,尤其是类Unix系统如Linux中,进程调度机制是保证多任务高效运行的核心。本文将深入探讨Linux操作系统内核的进程调度器——负责管理CPU资源分配的关键组件。我们会详细分析其调度策略、调度器的演进及其在多核处理器环境下的表现。通过剖析进程调度器的工作原理和设计哲学,旨在为读者提供一个清晰的视角来理解这一复杂的系统功能。
|
8天前
|
存储 Linux 数据安全/隐私保护
Linux进程间通信
Linux进程间通信
25 6
|
8天前
|
存储 Unix Linux
【Linux 系统】进程信号 -- 详解(下)
【Linux 系统】进程信号 -- 详解(下)
|
8天前
|
NoSQL Linux Shell
【Linux 系统】进程信号 -- 详解(上)
【Linux 系统】进程信号 -- 详解(上)
|
8天前
|
消息中间件 存储 安全
【Linux 系统】进程间通信(共享内存、消息队列、信号量)(下)
【Linux 系统】进程间通信(共享内存、消息队列、信号量)(下)
|
8天前
|
消息中间件 算法 Linux
【Linux 系统】进程间通信(共享内存、消息队列、信号量)(上)
【Linux 系统】进程间通信(共享内存、消息队列、信号量)(上)
|
15天前
|
消息中间件 存储 监控
RabbitMQ:分布式系统中的高效消息队列
RabbitMQ:分布式系统中的高效消息队列
|
15天前
|
消息中间件 分布式计算 监控
Python面试:消息队列(RabbitMQ、Kafka)基础知识与应用
【4月更文挑战第18天】本文探讨了Python面试中RabbitMQ与Kafka的常见问题和易错点,包括两者的基础概念、特性对比、Python客户端使用、消息队列应用场景及消息可靠性保证。重点讲解了消息丢失与重复的避免策略,并提供了实战代码示例,帮助读者提升在分布式系统中使用消息队列的能力。
46 2
|
15天前
|
消息中间件 Java
springboot整合消息队列——RabbitMQ
springboot整合消息队列——RabbitMQ
84 0
|
2天前
|
消息中间件 存储 网络协议
手写消息队列(基于RabbitMQ)
手写消息队列(基于RabbitMQ)

热门文章

最新文章

http://www.vxiaotou.com