我已编写此代码来制作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;
}
答案 0 :(得分:1)
我想我已经意识到问题是什么,或者更确切地说是错误。
这是我从here -
中读到的内容[参考mq_open()]
返回:队列成功时的有效消息队列描述符 创建,或-1(设置错误)。
因此,当实际发生错误时,我应该检查errno 的值。但是在上面的代码中,我只是打印该值而不管是否发生错误,因此它正在打印与errno中存储的某些 garbage 值对应的错误消息。
所以我的代码应该是这样的 -
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。我之前也读过有关启动mqueue服务器的内容 实际上使用消息队列?
我认为这是QNX的特殊要求。