您的位置:首页 > 运维架构

关于POSIX消息队列mq_open提示错误信息:Invalid argument的问题

2017-03-05 21:35 1446 查看

关于POSIX消息队列mq_open提示错误信息:Invalid argument的问题

1. 发现问题

在学习《Unix网络编程》卷二:进程间通信的第五章,p63页代码时,当调用mq_open函数时,总是提示错误信息Invalid argument。

最总将错误定位在mq_open函数的第四个参数&attr,当传值为NULL时,程序没错误提示,而&attr时,总是编译提示错误信息。

代码如下:

#include "unpipc.h"

struct mq_attr attr;

int main(int argc, char *argv[])
{
mqd_t mqd;
int flags, c;

flags = O_CREAT | O_RDWR;

while( (c = getopt(argc, argv, "em:z:")) != -1) {
switch (c) {
case 'e':
flags |= O_EXCL;
break;
case 'm':
attr.mq_maxmsg = atol(optarg);
break;
case 'z':
attr.mq_msgsize = atol(optarg);
break;
}
}
/*
printf("optind = %d\n", optind);
printf("argc = %d\n", argc);
printf("maxmsg #msgs = %ld\nmsgsize #bytes/msg= %ld\ncurmsg = %ld\n", attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
*/
if(optind != argc-1)
sys_err("usage:mqcreat [-e] [-m maxmsg -z msgsize] <name>");

if((attr.mq_maxmsg != 0 && attr.mq_msgsize == 0) ||
(attr.mq_maxmsg == 0 && attr.mq_msgsize != 0))
sys_err("must specify both -m maxmsg and -z msgsize");

mqd = mq_open(argv[optind], flags, FILE_MODE, &attr);

if(mqd == -1)
sys_err("mq_open err");

printf("mqd = %d\n", mqd);

mq_close(mqd);

return 0;
}


2. 及时总结

在man pages的第7章有关于消息队列的综述。

man mq_overview


有一段叫/proc interfaces的标签,说明了一些限制问题。

The following interfaces can be used to limit the amount of kernel memory  consumed by POSIX message queues and to set the default attributes for new message queues:


大意为:

以下接口可用于限制被POSIX消息队列消耗,和被设置新消息队列属性所消耗的内核内存量。


并且给出
/proc/sys/fs/mqueue/
目录下的文件详细解释

//文件内容如下:
msg_default         10
msg_max             10
msgsize_default     8192
msgsize_max         8192
queues_max          256
//本机系统为Ubuntu 16.04 LTS 64位


而UNP书上执行程序参数maxmsg为1024,已经超过系统限制的10。因此,总是提示错误信息:Invalid argument。

当将maxmsg参数大小小于等于10时,程序编译通过
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息