当前位置: 技术问答>linux和unix
请教一个关于posix消息队列的问题
来源: 互联网 发布时间:2016-03-07
本文导语: 问题: 使用mq_open 函数普通权限运行时提示错误mq_open:“Permission denied ” 在root权限下运行没有问题。我现在想在普通权限下运行,不知道怎么改,麻烦各位帮我看下,先谢过。。 写个小程序测...
问题: 使用mq_open 函数普通权限运行时提示错误mq_open:“Permission denied ”
在root权限下运行没有问题。我现在想在普通权限下运行,不知道怎么改,麻烦各位帮我看下,先谢过。。
写个小程序测试了下:
#include
#include
#include
#include
#define FILE_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
int main()
{
struct mq_attr attr;
attr.mq_flags = O_RDWR|O_CREAT;
attr.mq_maxmsg = 64;
attr.mq_msgsize = 64;
attr.mq_curmsgs = 0;
char key[14];
strncpy(key, "/test", strlen("/test") );
mq_unlink(key);
mqd_t value = mq_open(key , attr.mq_flags,FILE_MODE,&attr);
if ( value == -1){
perror("mq_openn");
} else{
perror("mq_openn");
}
return 0;
}
在root权限下运行没有问题。我现在想在普通权限下运行,不知道怎么改,麻烦各位帮我看下,先谢过。。
写个小程序测试了下:
#include
#include
#include
#include
#define FILE_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
int main()
{
struct mq_attr attr;
attr.mq_flags = O_RDWR|O_CREAT;
attr.mq_maxmsg = 64;
attr.mq_msgsize = 64;
attr.mq_curmsgs = 0;
char key[14];
strncpy(key, "/test", strlen("/test") );
mq_unlink(key);
mqd_t value = mq_open(key , attr.mq_flags,FILE_MODE,&attr);
if ( value == -1){
perror("mq_openn");
} else{
perror("mq_openn");
}
return 0;
}
|
attr.mq_maxmsg = 128;
128太大, 改为10就好了.
另外
attr.mq_msgsize = 8;
8也太小了吧, 难道消息长度8字节就够了?
具体的原因解释如下:
128太大, 改为10就好了.
另外
attr.mq_msgsize = 8;
8也太小了吧, 难道消息长度8字节就够了?
具体的原因解释如下:
mq_open
EINVAL O_CREAT was specified in oflag, and attr was not NULL, but
attr->mq_maxmsg or attr->mq_msqsize was invalid. Both of these
fields must be greater than zero. In a process that is unprivi-
leged (does not have the CAP_SYS_RESOURCE capability),
attr->mq_maxmsg must be less than or equal to the msg_max limit,
and attr->mq_msgsize must be less than or equal to the msg-
size_max limit. In addition, even in a privileged process,
attr->mq_maxmsg cannot exceed the HARD_MAX limit. (See
mq_overview(7) for details of these limits.)
mymtom@fc6$ cat /proc/sys/fs/mqueue/msg_max
10
mymtom@fc6$ cat /proc/sys/fs/mqueue/msgsize_max
8192