Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

mq_notify(2) [hpux man page]

mq_notify(2)							System Calls Manual						      mq_notify(2)

NAME
mq_notify - register/cancel a notification request with a message queue SYNOPSIS
DESCRIPTION
If the argument notification is not NULL, the system call registers the calling process to be notified of message arrival at an empty mes- sage queue associated with the message queue descriptor mqdes. The notification specified by the notification argument will be sent to the process when the message queue transitions from the empty state to the non-empty state. If the type of notification specified in notification->sigev_notify is then the signal specified in notification->sigev_signo will be sent to the process. If the flag is set for that signal number, then the value specified in notification->sigev_value will be the si_value com- ponent of the siginfo_t structure passed to the signal catching function at the time of signal delivery. At any time, only one process can be registered for notification with a message queue. If the calling process, or any other process has already registered for notification with the specified message queue, subsequent attempts to register with that queue will fail. If notification is NULL and the process is currently registered for notification with the specified message queue, the existing registra- tion is canceled. When the notification is sent to the registered process, its registration is removed. The message queue is then available for registra- tion. If there is some process blocked in waiting to receive a message from a message queue, the arrival of a message on the queue will satisfy the even if the queue has a registered notification request. The resulting behavior is as if the message queue remains empty, and no noti- fication is sent. To use this function, link in the realtime library by specifying on the compiler or linker command line. RETURN VALUE
returns the following values: Successful completion. Failure. is set to indicate the error. ERRORS
If fails, is set to one of the following values: [EAGAIN] The system lacks sufficient signal queuing resources to honor the request. [EBADF] The mqdes argument is not a valid message queue descriptor. [EBUSY] A process is already registered for notification with the message queue. [EINVAL] An attempt was made to cancel a non-existent notification request, or notification points to an invalid address. [ENOSYS] is not supported by the implementation. SEE ALSO
mq_open(2), mq_send(2), sigaction(2). signal(2), signal(5). STANDARDS CONFORMANCE
mq_notify(2)

Check Out this Related Man Page

MQ_NOTIFY(3)						     Linux Programmer's Manual						      MQ_NOTIFY(3)

NAME
mq_notify - register for notification when a message is available SYNOPSIS
#include <mqueue.h> int mq_notify(mqd_t mqdes, const struct sigevent *notification); Link with -lrt. DESCRIPTION
mq_notify() 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 notification argument is a pointer to a sigevent structure. For the definition and general details of this structure, see sigevent(7). If notification is a non-NULL pointer, then mq_notify() registers the calling process to receive message notification. The sigev_notify field of the sigevent structure to which notification points specifies how notification is to be performed. This field has one of the fol- lowing values: SIGEV_NONE A "null" notification: the calling process is registered as the target for notification, but when a message arrives, no notification is sent. SIGEV_SIGNAL Notify the process by sending the signal specified in sigev_signo. See sigevent(7) for general details. The si_code field of the siginfo_t structure will be set to SI_MESGQ. In addition, si_pid will be set to the PID of the process that sent the message, and si_uid will be set to the real user ID of the sending process. SIGEV_THREAD Upon message delivery, invoke sigev_notify_function 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 message queue. If notification is NULL, and the calling process is currently registered to receive notifications for this message queue, then the regis- tration is removed; another process can then register to receive a message notification for this queue. Message notification only occurs when a new message arrives and the queue was previously empty. If the queue was not empty at the time mq_notify() was called, then a notification will only occur 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 mq_receive(3), then any message notification registra- tion is ignored: the message is delivered to the process or thread calling mq_receive(3), and the message notification registration remains in effect. Notification occurs once: after a notification is delivered, the notification registration is removed, and another process can register for message notification. If the notified process wishes to receive the next notification, it can use mq_notify() 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 emp- tying the queue of messages without blocking once it is empty.) RETURN VALUE
On success mq_notify() 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 notification->sigev_notify is not one of the permitted values; or notification->sigev_notify is SIGEV_SIGNAL and notifica- tion->sigev_signo is not a valid signal number. ENOMEM Insufficient memory. POSIX.1-2008 says that an implementation may generate an EINVAL error if notification 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 message from the queue and then terminates the process. #include <pthread.h> #include <mqueue.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0) static void /* Thread start function */ tfunc(union sigval sv) { struct mq_attr attr; ssize_t nr; void *buf; mqd_t mqdes = *((mqd_t *) sv.sival_ptr); /* Determine max. msg size; allocate buffer to receive msg */ if (mq_getattr(mqdes, &attr) == -1) handle_error("mq_getattr"); buf = malloc(attr.mq_msgsize); if (buf == NULL) handle_error("malloc"); nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL); if (nr == -1) handle_error("mq_receive"); printf("Read %ld bytes from MQ ", (long) nr); free(buf); exit(EXIT_SUCCESS); /* Terminate the process */ } int main(int argc, char *argv[]) { mqd_t mqdes; struct sigevent not; if (argc != 2) { fprintf(stderr, "Usage: %s <mq-name> ", argv[0]); exit(EXIT_FAILURE); } mqdes = mq_open(argv[1], O_RDONLY); if (mqdes == (mqd_t) -1) handle_error("mq_open"); not.sigev_notify = SIGEV_THREAD; not.sigev_notify_function = tfunc; not.sigev_notify_attributes = NULL; not.sigev_value.sival_ptr = &mqdes; /* Arg. to thread func. */ if (mq_notify(mqdes, &not) == -1) handle_error("mq_notify"); pause(); /* Process will be terminated by thread function */ } SEE ALSO
mq_close(3), mq_getattr(3), mq_open(3), mq_receive(3), mq_send(3), mq_unlink(3), mq_overview(7), sigevent(7) COLOPHON
This page is part of release 3.27 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-09-19 MQ_NOTIFY(3)
Man Page