NAME
mqnotify - register for notification when a message is available SYNOPSIS
#include
int mqnotify(mqdt mqdes, const struct sigevent *sevp); Link with -lrt. DESCRIPTION mqnotify() allows the calling process to register or unregister for delivery of an asynchronous notification when a new message arrives on the empty message queue referred to by the descriptor mqdes. The sevp argument is a pointer to a sigevent structure. For the defi‐ nition and general details of this structure, see sigevent(7).
If sevp is a non-NULL pointer, then mqnotify() registers the calling process to receive message notification. The sigevnotify field of the sigevent structure to which sevp points specifies how notification is to be performed. This field has one of the following values: SIGEVNONE A "null" notification: the calling process is registered as the target for notification, but when a message arrives, no notifi‐ cation is sent. SIGEVSIGNAL Notify the process by sending the signal specified in sigevsigno. See sigevent(7) for general details. The sicode field of the siginfot structure will be set to SIMESGQ. In addition, sipid will be set to the PID of the process that sent the message, and siuid will be set to the real user ID of the sending process. SIGEVTHREAD Upon message delivery, invoke sigevnotifyfunction as if it were the start function of a new thread. See sigevent(7) for details. Only one process can be registered to receive notification from a mes‐ sage queue. If sevp is NULL, and the calling process is currently registered to receive notifications for this message queue, then the registration is removed; another process can then register to receive a message notifi‐ cation for this queue. Message notification occurs only when a new message arrives and the queue was previously empty. If the queue was not empty at the time mqnotify() was called, then a notification will occur only after the queue is emptied and a new message arrives. If another process or thread is waiting to read a message from an empty queue using mqreceive(3), then any message notification registration is ignored: the message is delivered to the process or thread calling mqreceive(3), and the message notification registration remains in effect. Notification occurs once: after a notification is delivered, the noti‐ fication registration is removed, and another process can register for message notification. If the notified process wishes to receive the next notification, it can use mqnotify() to request a further notifi‐ cation. This should be done before emptying all unread messages from the queue. (Placing the queue in nonblocking mode is useful for empty‐ ing the queue of messages without blocking once it is empty.) RETURN VALUE
On success mqnotify() returns 0; on error, -1 is returned, with errno set to indicate the error. ERRORS EBADF The descriptor specified in mqdes is invalid. EBUSY Another process has already registered to receive notification for this message queue.
EINVAL sevp->sigevnotify is not one of the permitted values; or
sevp->sigevnotify is SIGEVSIGNAL and sevp->sigevsigno is not a valid signal number. ENOMEM Insufficient memory.
POSIX.1-2008 says that an implementation may generate an EINVAL error if sevp is NULL, and the caller is not currently registered to receive notifications for the queue mqdes. CONFORMING TO
POSIX.1-2001. EXAMPLE The following program registers a notification request for the message
queue named in its command-line argument. Notification is performed by creating a thread. The thread executes a function which reads one mes‐ sage from the queue and then terminates the process.
#include
#include
#include
#include
#include
#define handleerror(msg) \ do { perror(msg); exit(EXITFAILURE); } while (0) static void /* Thread start function */ tfunc(union sigval sv) { struct mqattr attr; ssizet nr; void *buf; mqdt mqdes = *((mqdt *) sv.sivalptr); /* Determine max. msg size; allocate buffer to receive msg */
if (mqgetattr(mqdes, &attr) == -1) handleerror("mqgetattr"); buf = malloc(attr.mqmsgsize); if (buf == NULL) handleerror("malloc"); nr = mqreceive(mqdes, buf, attr.mqmsgsize, NULL);
if (nr == -1) handleerror("mqreceive");
printf("Read %ld bytes from MQ\n", (long) nr); free(buf); exit(EXITSUCCESS); /* Terminate the process */ } int main(int argc, char *argv[]) { mqdt mqdes; struct sigevent sev; if (argc != 2) {
fprintf(stderr, "Usage: %s
\n", argv[0]); exit(EXITFAILURE); } mqdes = mqopen(argv[1], ORDONLY); if (mqdes == (mqdt) -1) handleerror("mqopen"); sev.sigevnotify = SIGEVTHREAD; sev.sigevnotifyfunction = tfunc; sev.sigevnotifyattributes = NULL; sev.sigevvalue.sivalptr = &mqdes; /* Arg. to thread func. */
if (mqnotify(mqdes, &sev) == -1) handleerror("mqnotify"); pause(); /* Process will be terminated by thread function */ } SEE ALSO mqclose(3), mqgetattr(3), mqopen(3), mqreceive(3), mqsend(3), mqunlink(3), mqoverview(7), sigevent(7) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2010-10-04 MQNOTIFY(3)