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

来源:互联网 发布:淘宝企业店铺认证流程 编辑:程序博客网 时间:2024/05/22 01:53

Posix消息队列的属性使用如下结构存放:

1struct mq_attr
2{
3    longmq_flags; /*阻塞标志位,0为非阻塞(O_NONBLOCK)*/
4    longmq_maxmsg; /*队列所允许的最大消息条数*/
5    longmq_msgsize; /*每条消息的最大字节数*/
6    longmq_curmsgs; /*队列当前的消息条数*/
7};

队列可以在创建时由mq_open()函数的第四个参数指定mq_maxmsg,mq_msgsize。 如创建时没有指定则使用默认值,一旦创建,则不可再改变。

队列可以在创建后由mq_setattr()函数设置mq_flags

1#include <mqueue.h>
2/*取得消息队列属性,放到mqstat地fh*/
3int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat);
4/*设置消息队列属性,设置值由mqstat提供,原先值写入omqstat*/
5int mq_setattr(mqd_t mqdes, const struct mq_attr *mqstat, structmq_attr *omqstat);
6均返回:若成功则为0,若出错为-1

下面通过程序获取和设置消息队列的默认属性:
01#include <stdio.h>
02#include <stdlib.h>
03#include <mqueue.h>
04#include <sys/types.h>
05#include <sys/stat.h>
06#include <unistd.h>
07#include <fcntl.h>
08#include <errno.h>
09 
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 
14int main()
15{
16    mqd_t posixmq;
17    intrc = 0;
18 
19    structmq_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    printf("队列阻塞标志位:%ld\n", mqattr.mq_flags);
37    printf("队列允许最大消息数:%ld\n", mqattr.mq_maxmsg);
38    printf("队列消息最大字节数:%ld\n", mqattr.mq_msgsize);
39    printf("队列当前消息条数:%ld\n", mqattr.mq_curmsgs);
40 
41    rc = mq_close(posixmq);
42    if(0 != rc)
43    {
44        perror("关闭失败");
45        exit(1);
46    }
47 
48    rc = mq_unlink(MQ_NAME);
49    if(0 != rc)
50    {
51        perror("删除失败");
52        exit(1);
53    }
54 
55    return0;
56}

编译并执行:

1[infor@s123 PosixMq]$ gcc -o mqattr mqattr.c -lrt
2[infor@s123 PosixMq]$ ./mqattr
3队列阻塞标志位:0
4队列允许最大消息数:10
5队列消息最大字节数:8192
6队列当前消息条数:0

看下面如何设置:

 

01

#include <stdio.h>
02#include <stdlib.h>
03#include <mqueue.h>
04#include <sys/types.h>
05#include <sys/stat.h>
06#include <unistd.h>
07#include <fcntl.h>
08#include <errno.h>
09 
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 
14int main()
15{
16    mqd_t posixmq;
17    intrc = 0;
18 
19    structmq_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    if(-1 == posixmq)
27    {
28        perror("创建MQ失败");
29        exit(1);
30    }
31    mqattr.mq_flags = 0;
32    mq_setattr(posixmq, &mqattr, NULL);// mq_setattr()只关注mq_flags,adw
33   
34    // 获取消息队列的属性
35    rc = mq_getattr(posixmq, &mqattr);
36    if(-1 == rc)
37    {
38        perror("获取消息队列属性失败");
39        exit(1);
40    }
41    printf("队列阻塞标志位:%ld\n", mqattr.mq_flags);
42    printf("队列允许最大消息数:%ld\n", mqattr.mq_maxmsg);
43    printf("队列消息最大字节数:%ld\n", mqattr.mq_msgsize);
44    printf("队列当前消息条数:%ld\n", mqattr.mq_curmsgs);
45 
46    rc = mq_close(posixmq);
47    if(0 != rc)
48    {
49        perror("关闭失败");
50        exit(1);
51    }
52 
53    rc = mq_unlink(MQ_NAME);
54    if(0 != rc)
55    {
56        perror("删除失败");
57        exit(1);
58    }
59 
60    return0;
61}

编译并执行:

 

1

[infor@s123 PosixMq]$ gcc -o mqattr mqattr.c -lrt
2[infor@s123 PosixMq]$ ./mqattr
3队列阻塞标志位:0
4队列允许最大消息数:5
5队列消息最大字节数:8192
6队列当前消息条数:0
原创粉丝点击