且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

地址错误,带有mq_open

更新时间:2022-12-25 14:43:30

此呼叫

... = 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

在oflag中指定了

O_CREAT ,并且attr不为NULL,但是 attr-> mq_maxmsg attr-> mq_msqsize 无效.这两个字段都必须大于零.在没有特权的过程中(没有 CAP_SYS_RESOURCE 功能), attr-> mq_maxmsg 必须小于或等于 msg_max 限制,并且 attr-> mq_msgsize 必须小于或等于msgsize_max限制.此外,即使在特权进程中, attr-> mq_maxmsg 也不能超过 HARD_MAX 限制. (有关这些限制的详细信息,请参见mq_overview(7).)

attr的初始化达到了mq_maxmsg或/和mq_msgsize之一或两者的限制.阅读 man 7 mq_overview 有关如何找出限制的信息. /p>

I am trying to open a simple queue using mq_open but I keep getting error:

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

And I have no idea why.

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);
 }
}

I updated code as suggested but still getting error ("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;
}

This call

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

specifies too many parameters. The mode seems to have been specified twice.

It should be

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

or

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


Regarding EINVAL, the man mq_open states:

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.)

The initialisation of attr hits the limits for either one or both of mq_maxmsg or/and mq_msgsize. Read man 7 mq_overview on how to find out the limits.