#include <mqueue.h> int mq_notify(mqd_t mqdes, const struct sigevent *notification);
If notification is NULL and the process is currently registered for notification by the specified message queue, the existing registration shall be removed.
When the notification is sent to the registered process, its registration shall be removed. The message queue shall then be available for registration.
If a process has registered for notification of message arrival at a message queue and some thread is blocked in mq_receive() or mq_timedreceive() waiting to receive a message when a message arrives at the queue, the arriving message shall satisfy the appropriate mq_receive() or mq_timedreceive(), respectively. The resulting behavior is as if the message queue remains empty, and no notification shall be sent.
The mq_notify() function may fail if:
The following sections are informative.
#include <pthread.h>
#include <mqueue.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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 maximum msg size; allocate buffer to receive msg */
if (mq_getattr(mqdes, &attr) == -1) {
perror("mq_getattr");
exit(EXIT_FAILURE);
}
buf = malloc(attr.mq_msgsize);
if (buf == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
if (nr == -1) {
perror("mq_receive");
exit(EXIT_FAILURE);
}
printf("Read %ld bytes from message queue\n", (long) nr);
free(buf);
exit(EXIT_SUCCESS); /* Terminate the process */
}
int
main(int argc, char *argv[])
{
mqd_t mqdes;
struct sigevent not;
assert(argc == 2);
mqdes = mq_open(argv[1], O_RDONLY);
if (mqdes == (mqd_t) -1) {
perror("mq_open");
exit(EXIT_FAILURE);
}
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, ¬) == -1) {
perror("mq_notify");
exit(EXIT_FAILURE);
}
pause(); /* Process will be terminated by thread function */
}
The Base Definitions volume of POSIX.1-2017, <mqueue.h>
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .