Я пишу простое приложение для понимания очередей сообщений POSIX.Но приложение постоянно выдает ошибку «Bad file descriptor».
Благодаря пользователям 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;
}