mq_open 的错误地址

Bad address with mq_open

我正在尝试使用 mq_open 打开一个简单的队列,但我不断收到错误消息:

"Error while opening ... 
Bad address: Bad address"

我也不知道为什么。

int main(int argc, char **argv) {
    struct mq_attr attr;
    //max size of a message
    attr.mq_msgsize = MSG_SIZE; 
    attr.mq_flags = 0;
    //maximum of messages on queue
    attr.mq_maxmsg = 1024 ;
    dRegister = mq_open("/serverQRegister",O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR,0664, &attr);
    if(dRegister == -1)
    {
        perror("mq_open() failed");
        exit(1);
 }
}

我按照建议更新了代码,但仍然出现错误 ("invalid argument"):

#include <stdio.h>
#include <stdlib.h>
#include <mqueue.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include "serverDefinitions.h"
mqd_t dRegister;
int main(int argc, char **argv) {
    struct mq_attr attr;
    //setting all attributes to 0
    memset(&attr, 0, sizeof attr);
    //max size of a message
    attr.mq_msgsize = MSG_SIZE;  //MSG_SIZE = 4096
    attr.mq_flags = 0;
    //maximum of messages on queue
    attr.mq_maxmsg = 1024;
    dRegister = mq_open("/serverQRegister", O_RDONLY | O_CREAT, 0664, &attr);

    if (dRegister == -1) {
        perror("mq_open() failed");
        exit(1);
    }
    return 0;
}

这次通话

... = mq_open("/serverQRegister",O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR,0664, &attr);

指定的参数过多。 mode 似乎被指定了两次。

应该是

... = mq_open("/serverQRegister",O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR, &attr);

... = mq_open("/serverQRegister",O_RDONLY | O_CREAT, 0664, &attr);

关于 EINVAL,man 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 unprivileged (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 msgsize_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.)

attr 的初始化达到了 mq_maxmsg or/and mq_msgsize 之一或两者的限制。阅读 man 7 mq_overview 了解如何找出极限。

mq_open() 是一个 varadic 函数,它可以接受 2 个或 4 个参数,但你给它 5 个,这是错误的。

就这样

dRegister = mq_open("/serverQRegister",O_RDONLY | O_CREAT, 0664, &attr);

或使用符号名称,S_IRUSR | S_IWUSR 而不是八进制表示。

你还应该初始化attr的所有成员,如果你提供mq_attr,你必须同时设置mq_msgsize和mq_maxmsg,所以使它

struct mq_attr attr;
memset(&attr, 0, sizeof attr);
//max size of a message
attr.mq_msgsize = MSG_SIZE;
attr.mq_maxmsg = 10;

(注意,mq_maxmsg必须小于命令sysctl fs.mqueue.msg_max设置的值)