pmsg/mq_notify_via_thread.c

This is pmsg/mq_notify_via_thread.c, an example program file from the book, The Linux Programming Interface.

This file is not printed in the book; it is a supplementary file for Chapter 52.

The source code file is copyright 2010, Michael Kerrisk, and is licensed under the GNU General Public License, version 3.

In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.

  Cover of The Linux Programming Interface

Function list (Bold in this list means a function is not static)

/* mq_notify_via_thread.c

   Demonstrate message notification via threads on a POSIX message queue.

   See also mq_notify_thread.c.
*/
#include <pthread.h>
#include <mqueue.h>
#include <fcntl.h>              /* For definition of O_NONBLOCK */
#include "tlpi_hdr.h"

static void notifySetup(mqd_t *mqdp);
/* Drain all messages from the queue referred to by 'mqd' */

static void
drainQueue(mqd_t mqd)
{
    ssize_t numRead;
    char *msg;
    struct mq_attr attr;

    /* Determine mq_msgsize for message queue, and allocate
       a buffer of that size */

    if (mq_getattr(mqd, &attr) == -1)
        errExit("mq_getattr");

    msg = malloc(attr.mq_msgsize);
    if (msg == NULL)
        errExit("malloc");

    while ((numRead = mq_receive(mqd, msg, attr.mq_msgsize, NULL)) >= 0) {

        /* Do whatever processing is required for message */

        printf("Read %ld bytes\n", (long) numRead);
    }

    if (errno != EAGAIN)                /* Unexpected error */
        errExit("mq_receive");

    free(msg);
}
static void                     /* Thread notification function */
threadFunc(union sigval sv)
{
    mqd_t *mqdp;

    mqdp = sv.sival_ptr;

    /* Reregister for message notification */

    notifySetup(mqdp);
    drainQueue(*mqdp);
}
static void
notifySetup(mqd_t *mqdp)
{
    struct sigevent sev;

    sev.sigev_notify = SIGEV_THREAD;            /* Notify via thread */
    sev.sigev_notify_function = threadFunc;
    sev.sigev_notify_attributes = NULL;
            /* Could be pointer to pthread_attr_t structure */
    sev.sigev_value.sival_ptr = mqdp;           /* Argument to threadFunc() */

    if (mq_notify(*mqdp, &sev) == -1)
        errExit("mq_notify");
}
int
main(int argc, char *argv[])
{
    mqd_t mqd;

    if (argc != 2 || strcmp(argv[1], "--help") == 0)
        usageErr("%s /mq-name\n", argv[0]);

    mqd = mq_open(argv[1], O_RDONLY | O_NONBLOCK);
    if (mqd == (mqd_t) -1)
        errExit("mq_open");

    notifySetup(&mqd);
    drainQueue(mqd);    /* Handle possibility that messages were already
                           queued before we established notification */

    pause();            /* Wait for notifications via thread function */
}

 

Download pmsg/mq_notify_via_thread.c

Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.