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

关于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时,程序编译通过

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值