gpt4 book ai didi

c - 两个进程之间的 mq_notify - C

转载 作者:太空宇宙 更新时间:2023-11-03 23:52:50 26 4
gpt4 key购买 nike

我有两个进程,server.c 和 client.c 它们通过 POSIX 消息队列进行通信。客户端向队列发送一条消息,mq_notify 告诉服务器一条消息已添加到队列中。然后信号处理程序将接收并处理该消息。但是,我无法让它正常工作。从 client.c 添加消息永远不会发送信号处理程序(但是,如果我从 server.c 添加消息,它会设置处理程序)。服务器仍然可以从客户端接收放入队列的消息,但由于某些原因,这不会触发 server.c 的 mq_notify 中使用的处理程序。任何人都知道这是什么?这是每一方的相关示例代码:

客户端.c

/* queue has already been created, this opens it*/
msgq_id = mq_open(MSGQOBJ_NAME, O_RDWR);

if (msgq_id == (mqd_t)-1) {
perror("In mq_open()");
exit(1);
}

/* sending the message -- mq_send() */
mq_send(msgq_id, packet.mesg_data, strlen(packet.mesg_data), msgprio);

/* closing the queue -- mq_close() */
mq_close(msgq_id);

服务器.c

void handler()
{
/*for now it just prints that the signal was recieved*/
}
/*i opening the queue -- mq_open() */
msgq_id = mq_open(MSGQOBJ_NAME, O_RDWR);
if (msgq_id == (mqd_t)-1) {
perror("In mq_open()");
exit(1);
}



int main(){
.
.
.
.
/*Set up to be notifed when the queue gets something in it*/
signal(SIGUSR1, handler);
sigevent.sigev_signo = SIGUSR1;;
if(mq_notify (msgq_id, &sigevent) == -1)
{
if(errno == EBUSY)
printf("Another process has registered for notifications.\n");
_exit (EXIT_FAILURE);
}
//strcpy(packet2.mesg_data, "Hello world!");
//mq_send(msgq_id, packet2.mesg_data, strlen(packet2.mesg_data), 0);

while(1)
{
/*wait to be notified*/
}
.
.
.
}

这是否与它们是独立的进程有关?

最佳答案

啊哈!弄清楚了。通知被阻止,因为 server.c 正在等待 mq_receive。这意味着信号未被确认,因为进程正忙于等待 mq_receive。感谢所有看过我的问题的人。

关于c - 两个进程之间的 mq_notify - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15130235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com