【发布时间】:2015-07-12 18:25:01
【问题描述】:
我正在尝试使用 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);
}
}
我按照建议更新了代码,但仍然出现错误(“无效参数”):
#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;
}
【问题讨论】:
-
您可能希望将
printf("Error while opening ... \n"); perror(strerror(errno));替换为perror("mq_open() failed");并重新测试。perror()自动为errno附加“文本描述”。 -
“错误地址”是
EFAULT。根据mq_open()的文档,EFAULT不是由该函数设置的。 -
@alk 我更新了代码。我仍然收到“mq_open() failed: Bad address”。我正在运行:发行商 ID:elementary OS 描述:elementary OS Freya 版本:0.3
-
因此,您想查看您的平台/实现的文档以了解
mq_open()的含义,了解EFAULT在此上下文中的含义。 -
@alk 我在 Cloud9 上运行这个程序,仍然得到这个错误信息