2013-02-28 35 views
1

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

client.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); 

server.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*/ 
    } 
    . 
    . 
    . 
} 

这是否有些事情要和他们在一起单独的进程?

+0

您是否遇到任何错误?程序是否冻结? – 2013-02-28 08:12:40

回答

0

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