Search code examples
cposixmessagemessage-queue

mq_notify between two processes - C


I have two processes, server.c and client.c They are communicating via a POSIX Message Queue. The client sends a message to the queue and mq_notify tells the server a message has been added to the queue. The signal handler will then receive and process the message. However, I can't get it to work properly. Adding a message from client.c never sends of the signal handler (however if I add a message from the server.c, it sets of the handler). The server can still receive messages put in the queue from the client but for some reason this does not set off the handler used in mq_notify of server.c. Anyone have any idea what this is? here is the relevant sample code from each side:

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

Does this have something to do with them being separate processes?


Solution

  • AHA! Figured it out. The notification was blocked because server.c was waiting in mq_receive. This meant that the signal wasn't acknowledge because the processes was busy waiting for mq_receive. Thanks for anyone who looked at my question.