我正在尝试写入我的消息队列(mq_send)。
下面是我的代码,用于先打开队列,然后再写入队列。
打开:
int MQconnect (mqd_t * mq, char * name)
{
//printf("hello from MQconnect\n");
do{
mq=mq_open(name, O_WRONLY); //O_RDONLY
}while(mq==-1);
if(mq== -1){
return 0;
}
else
return 1;
// Connects to an existing mailslot for writing Uses mq as reference pointer, so that you can reach the handle from anywhere/
// Should return 1 on success and 0 on fail*/
}写作:
int MQwrite (mqd_t mq, void * sendBuffer) // (const char) sendBuffer
{
int nrOfBytes = mq_send(mq, (const char)sendBuffer, 1024, 10);
printf("\n%d", nrOfBytes);
return nrOfBytes; //nrOfBytes;
// Write a msg to a mailslot, return nr Uses mq as reference pointer, so that you can reach the handle from anywhere
// should return number of bytes read */
}打开可以正常工作,但我不能写入消息队列。
mq_send返回-1作为返回值,错误消息为:
errno: 9的值错误输出错误:错误文件描述符错误:文件描述符错误
对上述函数的调用是从这个函数中进行的:
void * mqClient(void * arg){
pthread_mutex_lock(&mutex);
char answer[20];
mqd_t mq_on_server;
usleep(1000);
int response = MQconnect(&mq_on_server, "/servermq");
if(response==0){
printf("something went wrong with MQconnect\n");
}
else{
//This loop continously scans planets given by the user
//while(!(0)){
printf("\nWrite to mailbox: ");
scanf("%s", answer);
MQwrite (mq_on_server, &answer);
int c;
while ( (c = getchar()) != '\n' && c != EOF);
//fflush(stdout);
//}
}
pthread_mutex_unlock(&mutex);
}有谁知道为什么我会得到这样的错误?我的朋友有完全相同的代码,对他来说,它是有效的。
发布于 2021-02-19 21:54:36
您忘记在MQconnect中取消对给定指针的引用。
int MQconnect (mqd_t * mq, char * name)
{
//printf("hello from MQconnect\n");
#if 0
/* wrong part */
do{
mq=mq_open(name, O_WRONLY); //O_RDONLY
}while(mq==-1);
if(mq== -1){
#else
/* fixed code */
do{
*mq=mq_open(name, O_WRONLY); //O_RDONLY
}while(*mq==-1);
if(*mq== -1){
#endif
return 0;
}
else
return 1;
// Connects to an existing mailslot for writing Uses mq as reference pointer, so that you can reach the handle from anywhere/
// Should return 1 on success and 0 on fail*/
}https://stackoverflow.com/questions/66278841
复制相似问题