无法设置POSIX消息队列属性 [英] cannot set POSIX message queue attribute
问题描述
我的环境:
- CentOS 6.5(64位内核)
- gcc 4.4.7 20120313
我正在尝试为POSIX消息队列设置属性,但是代码不会更改属性. 我只得到默认属性值.
I am trying to set attribute for POSIX message queue, but the code does not change the attribute. I only get default attribute values.
您能指出我的代码出了什么问题吗?
Could you point out what the problem is with my code?
我以用户(不是root)的身份执行a.out.
I execute the a.out as an user (not root).
#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
/*
gcc [file] -lrt
*/
static void showAttr(mqd_t mqd)
{
struct mq_attr attr;
mq_getattr(mqd, &attr);
printf("maxmsg = %d\n", attr.mq_maxmsg);
printf("msgsize = %d\n", attr.mq_msgsize);
printf("curmsgs = %d\n", attr.mq_curmsgs);
}
int main()
{
mqd_t mqd;
int flags;
int ret;
struct mq_attr attr;
flags = O_RDWR | O_CREAT;
attr.mq_flags = 0; // or O_NONBLOCK
attr.mq_maxmsg = 60;
attr.mq_msgsize = 120;
attr.mq_curmsgs = 0;
// POSIX IPC name should start with "/"
mqd = mq_open("/mq", flags,
// (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
0644,
&attr );
if (mqd < 0) {
printf("open failed\n");
exit(EXIT_FAILURE);
}
printf("open ok\n");
sleep(1);
showAttr(mqd);
ret = mq_close(mqd);
if (ret != 0) {
printf("open failed\n");
exit(EXIT_FAILURE);
}
printf("close ok\n");
return 0;
}
我发现以下代码有效. 但是,当我尝试设置attr.mq_maxmsg(= 60)时,mq_open失败.
I found that following code works. However, when I try to set attr.mq_maxmsg (=60), the mq_open fails.
#include <stdio.h>
#include <mqueue.h> // for message queue
#include <sys/stat.h>
#include <stdlib.h> // for EXIT_FAILURE
#include <string.h>
#include <errno.h>
/*
gcc [file] -lrt
*/
static void showAttr(mqd_t mqd)
{
struct mq_attr attr;
mq_getattr(mqd, &attr);
printf("maxmsg = %d\n", attr.mq_maxmsg);
printf("msgsize = %d\n", attr.mq_msgsize);
printf("curmsgs = %d\n", attr.mq_curmsgs);
}
int main()
{
mqd_t mqd;
int flags;
int ret;
struct mq_attr attr;
flags = O_RDWR | O_CREAT;
// POSIX IPC name should start with "/"
// 1. once open without attribute setting
mqd = mq_open("/mq", flags,
(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) );
mq_getattr(mqd, &attr);
mq_unlink("/mq");
mq_close(mqd);
// 2. set values of attribute
// attr.mq_maxmsg = 10;
attr.mq_msgsize = 120;
// 3. allocate attribute
mqd = mq_open("/mq", flags,
(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH),
// 0644,
&attr );
if (mqd < 0) {
printf("open failed %d\n", mqd);
exit(EXIT_FAILURE);
}
printf("open ok\n");
sleep(1);
showAttr(mqd);
ret = mq_close(mqd);
if (ret != 0) {
printf("open failed\n");
exit(EXIT_FAILURE);
}
printf("close ok\n");
return 0;
}
推荐答案
mq_open()
返回一个已经存在的队列,其属性是在创建队列时设置的.因此,O_CREAT标志无效,并且您指定的属性也将被忽略.
mq_open()
is returning an already existing queue whose attributes were set at the time it was created. The O_CREAT flag thus has no effect, and the attributes you specify are ignored as well.
在打开之前调用mq_unlink()
,也许还设置O_EXCL,看看有什么区别.
Call mq_unlink()
before your open, and perhaps set O_EXCL as well, and see the difference.
这篇关于无法设置POSIX消息队列属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!