如何解决mq notify 返回 -1,不通知何时应该
我的 mq_notify 有问题,当消息队列中有要读取的内容时没有通知我。
这是我的代码:
void * mqClient(void * arg){
pthread_mutex_lock(&mutex);
int errnum;
int retval;
char thread_id[30];
char thread_id2[30] = "/";
sprintf(thread_id,"%d",pthread_self());
strcat(thread_id2,thread_id);
printf("\nEnter planet name: ");
scanf("%s",planet.name);
printf("\nEnter planet X-position: ");
scanf("%lf",&planet.sx);
printf("\nEnter planet Y-position: ");
scanf("%lf",&planet.sy);
printf("\nEnter planet X-veLocity: ");
scanf("%lf",&planet.vx);
printf("\nEnter planet Y-veLocity: ");
scanf("%lf",&planet.vy);
printf("\nEnter planet mass: ");
scanf("%lf",&planet.mass);
printf("\nEnter planet life time: ");
scanf("%d",&planet.life);
strcpy(planet.pid,thread_id2);
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
MQwrite (mq_on_server,&planet);
strcat(thread_id2,thread_id);
//Create a messagequeue responsible for sending server-client specific messages
int response2 = MQcreate(&clientMQ,planet.pid);
if(response2 != 1)
printf("\nNO success in creating a message queue between client and server!");
else
{
printf("\nSUCCESS in creating a message queue (%s) between client and server!\n",planet.pid);
}
printf("\n---------------------------------------");
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = sigNotifier;
sev.sigev_notify_attributes = NULL;
sev.sigev_value.sival_ptr = &response2;
retval = mq_notify(response2,&sev);
printf("\n%d",retval);
if(retval < 0)
{
errnum = errno;
fprintf(stderr,"Value of errno: %d\n",errno);
perror("Error printed by perror");
fprintf(stderr,"Notification Failed: %s\n",strerror(errnum));
fflush(stdout);
}
/*int c;
while ( (c = getchar()) != '\n' && c != EOF);*/
pthread_mutex_unlock(&mutex);
return NULL;
}
和 sigNotifier:
static void sigNotifier(union sigval sv)
{
printf("Notification!");
char msg[1000];
MQread(clientMQ,msg);
printf("\nMessage received from server: %s",msg);
}
错误值:
Value of errno: 9
Error printed by perror: Bad file descriptor
Notification Failed: Bad file descriptor
我总是从 mq_notify 得到返回值 -1。我的代码中是否遗漏了什么?
服务器写入消息队列没有问题。我已经检查过客户端可以从消息队列中读取并且它可以但是我希望它只在有东西要读取时才从消息队列中读取。我的问题是程序卡在 MQread 上,直到有东西要读,但我不想要这种行为,因此我认为信号处理程序可以解决这个问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。