IPC通信:Posix消息队列的属性设置

来源:互联网 发布:什么软件好卖东西 编辑:程序博客网 时间:2024/05/16 13:19
Posix消息队列的属性使用如下结构存放:struct mq_attr  {      long mq_flags; /*阻塞标志位,0为非阻塞(O_NONBLOCK)*/     long mq_maxmsg; /*队列所允许的最大消息条数*/     long mq_msgsize; /*每条消息的最大字节数*/     long mq_curmsgs; /*队列当前的消息条数*/ }; 队列可以在创建时由mq_open()函数的第四个参数指定mq_maxmsg,mq_msgsize。 如创建时没有指定则使用默认值,一旦创建,则不可再改变。队列可以在创建后由mq_setattr()函数设置mq_flags #include <mqueue.h>  /*取得消息队列属性,放到mqstat地fh*/ int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat);  /*设置消息队列属性,设置值由mqstat提供,原先值写入omqstat*/ int mq_setattr(mqd_t mqdes, const struct mq_attr *mqstat, struct mq_attr *omqstat);  均返回:若成功则为0,若出错为-1 
复制代码

程序获取和设置消息队列的默认属性:

复制代码
 1 #include <stdio.h>   2 #include <stdlib.h>   3 #include <mqueue.h>   4 #include <sys/types.h>   5 #include <sys/stat.h>   6 #include <unistd.h>   7 #include <fcntl.h>   8 #include <errno.h>   9    10 #define MQ_NAME ("/tmp")  11 #define MQ_FLAG (O_RDWR | O_CREAT | O_EXCL) // 创建MQ的flag  12 #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) // 设定创建MQ的权限  13    14 int main()  15 {  16     mqd_t posixmq;  17     int rc = 0;  18    19     struct mq_attr mqattr;  20    21     // 创建默认属性的消息队列  22     posixmq = mq_open(MQ_NAME, MQ_FLAG, FILE_MODE, NULL);  23     if(-1 == posixmq)  24     {  25         perror("创建MQ失败");  26         exit(1);  27     }  28        29     // 获取消息队列的默认属性  30     rc = mq_getattr(posixmq, &mqattr);  31     if(-1 == rc)  32     {  33         perror("获取消息队列属性失败");  34         exit(1);  35     }  36 37     printf("队列阻塞标志位:%ld\n", mqattr.mq_flags);  38     printf("队列允许最大消息数:%ld\n", mqattr.mq_maxmsg);  39     printf("队列消息最大字节数:%ld\n", mqattr.mq_msgsize);  40     printf("队列当前消息条数:%ld\n", mqattr.mq_curmsgs);  41    42     rc = mq_close(posixmq);  43     if(0 != rc)  44     {  45         perror("关闭失败");  46         exit(1);  47     }  48    49     rc = mq_unlink(MQ_NAME);  50     if(0 != rc)  51     {  52         perror("删除失败");  53         exit(1);  54     }     55     return 0;  56 } 
复制代码

编译并执行:

复制代码
1 root@linux:/mnt/hgfs/C_libary# gcc -o attrmq attrmq.c -lrt2 root@linux:/mnt/hgfs/C_libary# ./attrmq3 队列阻塞标志位:04 队列允许最大消息数:105 队列消息最大字节数:81926 队列当前消息条数:07 root@linux:/mnt/hgfs/C_libary# 
复制代码

设置消息队列的属性:

复制代码
 1 #include <stdio.h>   2 #include <stdlib.h>   3 #include <mqueue.h>   4 #include <sys/types.h>   5 #include <sys/stat.h>   6 #include <unistd.h>   7 #include <fcntl.h>   8 #include <errno.h>   9    10 #define MQ_NAME ("/tmp")  11 #define MQ_FLAG (O_RDWR | O_CREAT | O_EXCL) // 创建MQ的flag  12 #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) // 设定创建MQ的权限  13    14 int main()  15 {  16     mqd_t posixmq;  17     int rc = 0;  18    19     struct mq_attr mqattr;  20   21     // 创建默认属性的消息队列  22     mqattr.mq_maxmsg = 5; // 注意不能超过系统最大限制  23     mqattr.mq_msgsize = 8192;  24     //posixmq = mq_open(MQ_NAME, MQ_FLAG, FILE_MODE, NULL);  25     posixmq = mq_open(MQ_NAME, MQ_FLAG, FILE_MODE, &mqattr);  26 27     if(-1 == posixmq)  28     {  29         perror("创建MQ失败");  30         exit(1);  31     }  32     33     mqattr.mq_flags = 0;  34     mq_setattr(posixmq, &mqattr, NULL);// mq_setattr()只关注mq_flags,adw  35        36     // 获取消息队列的属性  37     rc = mq_getattr(posixmq, &mqattr);  38     if(-1 == rc)  39     {  40         perror("获取消息队列属性失败");  41         exit(1);  42     }  43 44     printf("队列阻塞标志位:%ld\n", mqattr.mq_flags);  45     printf("队列允许最大消息数:%ld\n", mqattr.mq_maxmsg);  46     printf("队列消息最大字节数:%ld\n", mqattr.mq_msgsize);  47     printf("队列当前消息条数:%ld\n", mqattr.mq_curmsgs);  48    49     rc = mq_close(posixmq);  50     if(0 != rc)  51     {  52         perror("关闭失败");  53         exit(1);  54     }    55 56     rc = mq_unlink(MQ_NAME);  57     if(0 != rc)  58     {  59         perror("删除失败");  60         exit(1);  61     }62          63     return 0;  64 } 
复制代码

编译运行:

复制代码
1 root@linux:/mnt/hgfs/C_libary# gcc -o setattrmq setattrmq.c -lrt2 root@linux:/mnt/hgfs/C_libary# ./setattrmq3 队列阻塞标志位:04 队列允许最大消息数:55 队列消息最大字节数:81926 队列当前消息条数:0
原创粉丝点击