我編寫了一個簡單的應用程序來理解POSIX消息隊列。但該應用程序不斷給「錯誤的文件描述符」錯誤。消息隊列錯誤文件描述符錯誤
感謝stackoverflow用戶。我們找到解決方案。以下是更新的代碼。
#include <mqueue.h>
#include <string.h>
#include <iostream>
#include <errno.h>
using namespace std;
int main(int argc, char *argv[])
{
mqd_t messageQueue;
mq_attr attr;
messageQueue = mq_open("/test",O_RDWR|O_CREAT,0664,&attr);
attr.mq_maxmgs = 10;
attr.mq_msgsize = 4;
char c;
int pid = fork();
//client
if(pid == 0) {
if(mq_receive(messageQueue,&c,1,0) == -1)
cout<<"Error:"<<strerror(errno)<<"\n";
cout<<"Received:"<<c<<"\n";
}
//server
else if(pid > 0) {
c = 'a';
if(mq_send(messageQueue,&c,1,0) == -1)
cout<<"Error:"<<strerror(errno)<<"\n";
cout<<"Send:"<<c<<"\n";
mq_close(messageQueue);
}
else {
cout<<"Fork error\n";
}
return 0;
}
檢查'mq_open'的返回值以瞭解它是否有效? – Mat 2012-03-27 08:04:30
@Mat我剛試過。它不工作。它說「無效的參數」 – onurozcelik 2012-03-27 08:11:17
那是你的問題。閱讀手冊頁,看看它說'EINVAL'。 – Mat 2012-03-27 08:12:12