我已经编写了这段代码来制作一个 posix 消息队列。但我收到错误消息“功能未实现”。
Q1。是平台相关的问题吗? [正在使用 Ubuntu 10.10] 我在某处读到我需要重建内核以启用消息队列!?
Q2。我还阅读了一些关于在实际使用消息队列之前启动 mqueue 服务器的内容?
谁能解释一下..
#include <mqueue.h> /* message queue stuff */
#include <iostream>
#include <unistd.h> /* for getopt() */
#include <errno.h> /* errno and perror */
#include <fcntl.h> /* O_flags */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main(int argc, char **argv)
{
mqd_t msgQueueDescriptor;
mq_attr attr;
char Msg[]="msg";
attr.mq_maxmsg = 10;
attr.mq_msgsize = sizeof(Msg);
attr.mq_flags = 0;
msgQueueDescriptor = mq_open("/myQueue", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH , attr );
cout << msgQueueDescriptor << " " << errno << " " << strerror(errno);
mq_close(msgQueueDescriptor);
return 0;
}
最佳答案
我想我已经意识到问题所在,或者更确切地说是一个侧面的错误。
这是我从here读到的-
[ In reference to mq_open( ) ]
Returns: A valid message queue descriptor if the queue is successfully created, or -1 (errno is set).
所以,我应该在实际发生错误时仅检查errno 的值!。但在上面的代码中,无论是否发生错误,我都只是打印值,因此它打印的是与存储在 errno 中的一些垃圾值相对应的错误消息。
所以我的代码应该是这样的-
if ((msgQueueDescriptor = mq_open("/myQueue", O_RDWR|O_CREAT, 0664 ,NULL ) == -1))
{
cout << msgQueueDescriptor << " " << errno << " " << strerror(errno);
}
else
{
cout << "All is well" ;
}
我是不是把自己弄傻了:p
PS:就 Ubuntu 10.10 上启用的消息队列而言,我检查了“n.m.”提到的标志,它们非常启用,我现在可以使用消息队列了。谢谢大家 - larsmans、VJovic、n.m.、Joachim Pileborg、jørgensen。
关于我的第二个问题
Q2. I also read something about starting the mqueue server before actually using message queues ?
我认为这是专门针对 QNX 的要求。
关于c++ - 实现 posix 消息队列时出错 - "Function not implemented",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8879408/