zoukankan      html  css  js  c++  java
  • [转]Linux进程通信之POSIX消息队列

    进程间的消息队列可以用这个实现,学习了下。

    http://blog.csdn.net/anonymalias/article/details/9799645?utm_source=tuicool&utm_medium=referral

    消息队列是Linux IPC中很常用的一种通信方式,它通常用来在不同进程间发送特定格式的消息数据。

    消息队列和之前讨论过的管道和FIFO有很大的区别,主要有以下两点:

    • 一个进程向消息队列写入消息之前,并不需要某个进程在该队列上等待该消息的到达,而管道和FIFO是相反的,进程向其中写消息时,管道和FIFO必需已经打开来读,那么内核会产生SIGPIPE信号(感谢shanshan_fangfang的指正)。
    •  IPC的持续性不同。管道和FIFO随进程的持续性,当管道和FIFO最后一次关闭发生时,仍在管道和FIFO中的数据会被丢弃。消息队列是随内核的持续性,即一个进程向消息队列写入消息后,然后终止,另外一个进程可以在以后某个时刻打开该队列读取消息。只要内核没有重新自举,消息队列没有被删除。

    消息队列中的每条消息通常具有以下属性:

    • 一个表示优先级的整数;
    • 消息的数据部分的长度;
    •  消息数据本身;

    POSIX消息队列的一个可能的设计是一个如下图所示的消息链表,链表头部有消息队列的属性信息。


    1消息队列的可能布局

    1 POSIX消息队列的创建和关闭

    POSIX消息队列的创建,关闭和删除用到以下三个函数接口:

    1. #include <mqueue.h>  
    2. mqd_t mq_open(const char *name, int oflag, /* mode_t mode, struct mq_attr *attr */);  
    3.                        //成功返回消息队列描述符,失败返回-1  
    4. mqd_t mq_close(mqd_t mqdes);  
    5. mqd_t mq_unlink(const char *name);  
    6.                            //成功返回0,失败返回-1  

    mq_open用于打开或创建一个消息队列。

    name:表示消息队列的名字,它符合POSIX IPC的名字规则。

    oflag:表示打开的方式,和open函数的类似。有必须的选项:O_RDONLYO_WRONLYO_RDWR,还有可选的选项:O_NONBLOCKO_CREATO_EXCL

    mode:是一个可选参数,在oflag中含有O_CREAT标志且消息队列不存在时,才需要提供该参数。表示默认访问权限。可以参考open

    attr:也是一个可选参数,在oflag中含有O_CREAT标志且消息队列不存在时才需要。该参数用于给新队列设定某些属性,如果是空指针,那么就采用默认属性。

    mq_open返回值是mqd_t类型的值,被称为消息队列描述符。在Linux 2.6.18中该类型的定义为整型:

    1. #include <bits/mqueue.h>  
    2. typedef int mqd_t;  

    mq_close用于关闭一个消息队列,和文件的close类型,关闭后,消息队列并不从系统中删除。一个进程结束,会自动调用关闭打开着的消息队列。

    mq_unlink用于删除一个消息队列。消息队列创建后只有通过调用该函数或者是内核自举才能进行删除。每个消息队列都有一个保存当前打开着描述符数的引用计数器,和文件一样,因此本函数能够实现类似于unlink函数删除一个文件的机制。

    POSIX消息队列的名字所创建的真正路径名和具体的系统实现有关,关于具体POSIX IPC的名字规则可以参考《UNIX 网络编程 卷2:进程间通信》的P14

    经过测试,在Linux 2.6.18中,所创建的POSIX消息队列不会在文件系统中创建真正的路径名。且POSIX的名字只能以一个’/’开头,名字中不能包含其他的’/’。

    2 POSIX消息队列的属性

    POSIX标准规定消息队列属性mq_attr必须要含有以下四个内容:

    1. long    mq_flags //消息队列的标志:0或O_NONBLOCK,用来表示是否阻塞   
    2. long    mq_maxmsg  //消息队列的最大消息数  
    3. long    mq_msgsize  //消息队列中每个消息的最大字节数  
    4. long    mq_curmsgs  //消息队列中当前的消息数目  

    Linux 2.6.18中mq_attr结构的定义如下:

    1. #include <bits/mqueue.h>  
    2. struct mq_attr  
    3. {  
    4.   long int mq_flags;      /* Message queue flags.  */  
    5.   long int mq_maxmsg;   /* Maximum number of messages.  */  
    6.   long int mq_msgsize;   /* Maximum message size.  */  
    7.   long int mq_curmsgs;   /* Number of messages currently queued.  */  
    8.   long int __pad[4];  
    9. };  

    POSIX消息队列的属性设置和获取可以通过下面两个函数实现:

    1. #include <mqueue.h>  
    2. mqd_t mq_getattr(mqd_t mqdes, struct mq_attr *attr);  
    3. mqd_t mq_setattr(mqd_t mqdes, struct mq_attr *newattr, struct mq_attr *oldattr);  
    4.                                //成功返回0,失败返回-1  

    mq_getattr用于获取当前消息队列的属性,mq_setattr用于设置当前消息队列的属性。其中mq_setattr中的oldattr用于保存修改前的消息队列的属性,可以为空。

    mq_setattr可以设置的属性只有mq_flags,用来设置或清除消息队列的非阻塞标志。newattr结构的其他属性被忽略。mq_maxmsgmq_msgsize属性只能在创建消息队列时通过mq_open来设置。mq_open只会设置该两个属性,忽略另外两个属性mq_curmsgs属性只能被获取而不能被设置。

    下面是测试代码:

    1. #include <iostream>  
    2. #include <cstring>  
    3.   
    4. #include <errno.h>  
    5. #include <unistd.h>  
    6. #include <fcntl.h>  
    7. #include <mqueue.h>  
    8.   
    9. using namespace std;  
    10.   
    11. int main()  
    12. {  
    13.     mqd_t mqID;  
    14.     mqID = mq_open("/anonymQueue", O_RDWR | O_CREAT, 0666, NULL);  
    15.   
    16.     if (mqID < 0)  
    17.     {  
    18.         cout<<"open message queue error..."<<strerror(errno)<<endl;  
    19.         return -1;  
    20.     }  
    21.   
    22.     mq_attr mqAttr;  
    23.     if (mq_getattr(mqID, &mqAttr) < 0)  
    24.     {  
    25.         cout<<"get the message queue attribute error"<<endl;  
    26.         return -1;  
    27.     }  
    28.   
    29.     cout<<"mq_flags:"<<mqAttr.mq_flags<<endl;  
    30.     cout<<"mq_maxmsg:"<<mqAttr.mq_maxmsg<<endl;  
    31.     cout<<"mq_msgsize:"<<mqAttr.mq_msgsize<<endl;  
    32.     cout<<"mq_curmsgs:"<<mqAttr.mq_curmsgs<<endl;  
    33. }  

    Linux 2.6.18中执行结果是:

    1. mq_flags:0  
    2. mq_maxmsg:10  
    3. mq_msgsize:8192  
    4. mq_curmsgs:0  

    3 POSIX消息队列的使用

    POSIX消息队列可以通过以下两个函数来进行发送和接收消息:

    1. #include <mqueue.h>  
    2. mqd_t mq_send(mqd_t mqdes, const char *msg_ptr,  
    3.                       size_t msg_len, unsigned msg_prio);  
    4.                      //成功返回0,出错返回-1  
    5.   
    6. mqd_t mq_receive(mqd_t mqdes, char *msg_ptr,  
    7.                       size_t msg_len, unsigned *msg_prio);  
    8.                      //成功返回接收到消息的字节数,出错返回-1  
    9.   
    10. #ifdef __USE_XOPEN2K  
    11. mqd_t mq_timedsend(mqd_t mqdes, const char *msg_ptr,  
    12.                       size_t msg_len, unsigned msg_prio,  
    13.                       const struct timespec *abs_timeout);  
    14.   
    15. mqd_t mq_timedreceive(mqd_t mqdes, char *msg_ptr,  
    16.                       size_t msg_len, unsigned *msg_prio,  
    17.                       const struct timespec *abs_timeout);  
    18. #endif  

    mq_send向消息队列中写入一条消息,mq_receive从消息队列中读取一条消息。

    mqdes:消息队列描述符;

    msg_ptr:指向消息体缓冲区的指针;

    msg_len:消息体的长度,其中mq_receive的该参数不能小于能写入队列中消息的最大大小,即一定要大于等于该队列的mq_attr结构中mq_msgsize的大小。如果mq_receive中的msg_len小于该值,就会返回EMSGSIZE错误。POXIS消息队列发送的消息长度可以为0

    msg_prio:消息的优先级;它是一个小于MQ_PRIO_MAX的数,数值越大,优先级越高。POSIX消息队列在调用mq_receive时总是返回队列中最高优先级的最早消息。如果消息不需要设定优先级,那么可以在mq_send是置msg_prio0mq_receivemsg_prio置为NULL

    还有两个XSI定义的扩展接口限时发送和接收消息的函数:mq_timedsendmq_timedreceive函数。默认情况下mq_sendmq_receive是阻塞进行调用,可以通过mq_setattr来设置为O_NONBLOCK

    下面是消息队列使用的测试代码:

    1. #include <iostream>  
    2. #include <cstring>  
    3. #include <errno.h>  
    4.   
    5. #include <unistd.h>  
    6. #include <fcntl.h>  
    7. #include <mqueue.h>  
    8.   
    9. using namespace std;  
    10.   
    11. int main()  
    12. {  
    13.     mqd_t mqID;  
    14.     mqID = mq_open("/anonymQueue", O_RDWR | O_CREAT | O_EXCL, 0666, NULL);  
    15.   
    16.     if (mqID < 0)  
    17.     {  
    18.         if (errno == EEXIST)  
    19.         {  
    20.             mq_unlink("/anonymQueue");  
    21.             mqID = mq_open("/anonymQueue", O_RDWR | O_CREAT, 0666, NULL);  
    22.         }  
    23.         else  
    24.         {  
    25.             cout<<"open message queue error..."<<strerror(errno)<<endl;  
    26.             return -1;  
    27.         }  
    28.     }  
    29.   
    30.     if (fork() == 0)  
    31.     {  
    32.         mq_attr mqAttr;  
    33.         mq_getattr(mqID, &mqAttr);  
    34.   
    35.         char *buf = new char[mqAttr.mq_msgsize];  
    36.   
    37.         for (int i = 1; i <= 5; ++i)  
    38.         {  
    39.             if (mq_receive(mqID, buf, mqAttr.mq_msgsize, NULL) < 0)  
    40.             {  
    41.                 cout<<"receive message  failed. ";  
    42.                 cout<<"error info:"<<strerror(errno)<<endl;  
    43.                 continue;  
    44.             }  
    45.   
    46.             cout<<"receive message "<<i<<": "<<buf<<endl;     
    47.         }  
    48.         exit(0);  
    49.     }  
    50.   
    51.     char msg[] = "yuki";  
    52.     for (int i = 1; i <= 5; ++i)  
    53.     {  
    54.         if (mq_send(mqID, msg, sizeof(msg), i) < 0)  
    55.         {  
    56.             cout<<"send message "<<i<<" failed. ";  
    57.             cout<<"error info:"<<strerror(errno)<<endl;  
    58.         }  
    59.         cout<<"send message "<<i<<" success. "<<endl;     
    60.   
    61.         sleep(1);  
    62.     }  
    63. }                       

    Linux 2.6.18下的执行结构如下:

    1. send message 1 success.   
    2. receive message 1: yuki  
    3. send message 2 success.   
    4. receive message 2: yuki  
    5. send message 3 success.   
    6. receive message 3: yuki  
    7. send message 4 success.   
    8. receive message 4: yuki  
    9. send message 5 success.   
    10. receive message 5: yuki  

    4 POSIX消息队列的限制

    POSIX消息队列本身的限制就是mq_attr中的mq_maxmsgmq_msgsize,分别用于限定消息队列中的最大消息数和每个消息的最大字节数。在前面已经说过了,这两个参数可以在调用mq_open创建一个消息队列的时候设定。当这个设定是受到系统内核限制的。

    下面是在Linux 2.6.18下shell对启动进程的POSIX消息队列大小的限制:

    1. # ulimit -a |grep message  
    2. POSIX message queues     (bytes, -q) 819200  

    限制大小为800KB该大小是整个消息队列的大小,不仅仅是最大消息数*消息的最大大小;还包括消息队列的额外开销。前面我们知道Linux 2.6.18下POSIX消息队列默认的最大消息数和消息的最大大小分别为:

    1. mq_maxmsg = 10  
    2. mq_msgsize = 8192  

    为了说明上面的限制大小包括消息队列的额外开销,下面是测试代码:

    1. #include <iostream>  
    2. #include <cstring>  
    3. #include <errno.h>  
    4.   
    5. #include <unistd.h>  
    6. #include <fcntl.h>  
    7. #include <mqueue.h>  
    8.   
    9. using namespace std;  
    10.   
    11. int main(int argc, char **argv)  
    12. {  
    13.     mqd_t mqID;  
    14.     mq_attr attr;  
    15.     attr.mq_maxmsg = atoi(argv[1]);  
    16.     attr.mq_msgsize = atoi(argv[2]);  
    17.   
    18.     mqID = mq_open("/anonymQueue", O_RDWR | O_CREAT | O_EXCL, 0666, &attr);  
    19.   
    20.     if (mqID < 0)  
    21.     {  
    22.         if (errno == EEXIST)  
    23.         {  
    24.             mq_unlink("/anonymQueue");  
    25.             mqID = mq_open("/anonymQueue", O_RDWR | O_CREAT, 0666, &attr);  
    26.   
    27.             if(mqID < 0)  
    28.             {  
    29.                 cout<<"open message queue error..."<<strerror(errno)<<endl;  
    30.                 return -1;  
    31.             }  
    32.         }  
    33.         else  
    34.         {  
    35.             cout<<"open message queue error..."<<strerror(errno)<<endl;  
    36.             return -1;  
    37.         }  
    38.     }  
    39.   
    40.     mq_attr mqAttr;  
    41.     if (mq_getattr(mqID, &mqAttr) < 0)  
    42.     {  
    43.         cout<<"get the message queue attribute error"<<endl;  
    44.         return -1;  
    45.     }  
    46.   
    47.     cout<<"mq_flags:"<<mqAttr.mq_flags<<endl;  
    48.     cout<<"mq_maxmsg:"<<mqAttr.mq_maxmsg<<endl;  
    49.     cout<<"mq_msgsize:"<<mqAttr.mq_msgsize<<endl;  
    50.     cout<<"mq_curmsgs:"<<mqAttr.mq_curmsgs<<endl;   
    51. }  

    下面进行创建消息队列时设置最大消息数和消息的最大大小进行测试:

    1. [root@idcserver program]# g++ -g test.cpp -lrt  
    2. [root@idcserver program]# ./a.out 10 81920  
    3. open message queue error...Cannot allocate memory  
    4. [root@idcserver program]# ./a.out 10 80000  
    5. open message queue error...Cannot allocate memory  
    6. [root@idcserver program]# ./a.out 10 70000  
    7. open message queue error...Cannot allocate memory  
    8. [root@idcserver program]# ./a.out 10 60000  
    9. mq_flags:0  
    10. mq_maxmsg:10  
    11. mq_msgsize:60000  
    12. mq_curmsgs:0  

    从上面可以看出消息队列真正存放消息数据的大小是没有819200B的。可以通过修改该限制参数,来改变消息队列的所能容纳消息的数量。可以通过下面方式来修改限制,但这会在shell启动进程结束后失效,可以将设置写入开机启动的脚本中执行,例如.bashrcrc.local

    1. [root@idcserver ~]# ulimit -q 1024000000  
    2. [root@idcserver ~]# ulimit -a |grep message  
    3. POSIX message queues     (bytes, -q) 1024000000  

    下面再次测试可以设置的消息队列的属性。

    1. [root@idcserver program]# ./a.out 10 81920  
    2. mq_flags:0  
    3. mq_maxmsg:10  
    4. mq_msgsize:81920  
    5. mq_curmsgs:0  
    6. [root@idcserver program]# ./a.out 10 819200  
    7. mq_flags:0  
    8. mq_maxmsg:10  
    9. mq_msgsize:819200  
    10. mq_curmsgs:0  
    11. [root@idcserver program]# ./a.out 1000 8192    
    12. mq_flags:0  
    13. mq_maxmsg:1000  
    14. mq_msgsize:8192  
    15. mq_curmsgs:0  

    POSIX消息队列在实现上还有另外两个限制:

    MQ_OPEN_MAX:一个进程能同时打开的消息队列的最大数目,POSIX要求至少为8;

    MQ_PRIO_MAX:消息的最大优先级,POSIX要求至少为32;

    Aug 7, 2013 AM 08:53 @lab

  • 相关阅读:
    『深度应用』NLP机器翻译深度学习实战课程·零(基础概念)
    Sorl初始
    Hadoop简介
    lucene的分词器宝典
    Lucene 更新、删除、分页操作以及IndexWriter优化
    Lucene 初步 之 HelloWorld
    lucene介绍和存储介绍
    Spring 集成rabbiatmq
    RabbitMQ 之消息确认机制(事务+Confirm)
    RabbitMQ 的路由模式 Topic模式
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/4919845.html
Copyright © 2011-2022 走看看