The Linux Programming Interface

(nextflipdebug5) #1

1080 Chapter 52


notification has occurred. At this point, the process will have been deregis-
tered for message notification.
b) Call mq_notify() to reregister this process to receive message notification u.
c) Execute a while loop that drains the queue by reading as many messages as
possible i.

Listing 52-6: Receiving message notification via a signal
––––––––––––––––––––––––––––––––––––––––––––––––––––––pmsg/mq_notify_sig.c
#include <signal.h>
#include <mqueue.h>
#include <fcntl.h> /* For definition of O_NONBLOCK */
#include "tlpi_hdr.h"

#define NOTIFY_SIG SIGUSR1

static void
handler(int sig)
{
/* Just interrupt sigsuspend() */
}

int
main(int argc, char *argv[])
{
struct sigevent sev;
mqd_t mqd;
struct mq_attr attr;
void *buffer;
ssize_t numRead;
sigset_t blockMask, emptyMask;
struct sigaction sa;

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

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

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

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

r sigemptyset(&blockMask);
sigaddset(&blockMask, NOTIFY_SIG);
if (sigprocmask(SIG_BLOCK, &blockMask, NULL) == -1)
errExit("sigprocmask");
Free download pdf