Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I'm using mq_notify to be notified about events on a message queue. But my registered notifier function is not being called. I'm pasting my code snippet below: ----------------------------------------------------------------------- static ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    19

    mq_notify not notifying about an event

    Hi,

    I'm using mq_notify to be notified about events on a message queue.
    But my registered notifier function is not being called.
    I'm pasting my code snippet below:

    -----------------------------------------------------------------------

    static void sigNotifier(union sigval sv)
    {

    printf ("I'm called.\n");

    }


    int main()
    {
    mqd_t queueID = 0;
    message_t msg;
    int retval;
    struct mq_attr attr;
    struct sigevent sev;

    attr.mq_msgsize = MSG_SIZE;
    attr.mq_maxmsg = 30;

    errno = 0;

    queueID = mq_open(MSG_QUEUE_NAME, O_RDONLY, 0666, &attr);

    if (queueID == -1) {
    printf ("Message queue open failed: %d\n", errno);
    }

    sev.sigev_notify = SIGEV_THREAD;
    sev.sigev_notify_function = sigNotifier;
    sev.sigev_notify_attributes = NULL;
    sev.sigev_value.sival_ptr = &queueID;

    retval = mq_notify(queueID, &sev);
    if (retval < 0) {
    printf ("Notification failed: %d\n", errno);
    }

    while (1);

    }

    Did I miss something, Please help me.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    From the mq_notify manpage:
    Code:
           SIGEV_THREAD
                  Deliver  notification by invoking notification->sigev_thread_function as the start function of
                  a new thread.  The function is invoked with notification->sigev_value as  its  sole  argument.
                  If notification->sigev_notify_attributes is not NULL, then it should point to a pthread_attr_t
                  structure that defines attributes for the thread.
    
           Only one process can be registered to receive notification from a message queue.
    So, you are using SIGEV_THREAD as the notification method. That means your function is invoked in a new pthread. The mq_notify() method only registers the function. It wil not be invoked until a message is received in the queue. Where are you being sent a message?

    Another problem is that you are passing the address of a function-local (automatic) variable, &queueID, in the sigev_value field. This is invalid since there is no way the thread-dispatched notification method can know if the address is still valid. You either should use a translation-unit static variable, a function-unit static variable, a global variable, or a heap-allocated pointer.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    May 2010
    Posts
    19

    @Rubberman

    Thanks for your reply.
    I was sending message from another process. mq_send() was a success there. I verified it by capturing the message using mq_receive().

    and about the sigev_value field, you are right about not using the pointer from heap memory. But, I guess it has nothing to do with the problem I'm facing. I'm not using that value anyway.

  4. #4
    Just Joined!
    Join Date
    May 2010
    Posts
    19

    Sorry, It works

    man page says-
    "Message notification only occurs when a new message arrives and the queue was previously empty"

    I missed this point.

  5. #5
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by arunalapati View Post
    man page says-
    "Message notification only occurs when a new message arrives and the queue was previously empty"

    I missed this point.
    Glad to hear it! Sometimes things just function as designed, not how we think they should!
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...