mq_notify(3)
β¨ NAME β¨
mq_notify β register for notification when a message is available π
π LIBRARY π
Real-time library β that's librt, link it with -lrt. Periodt.
π SYNOPSIS π
#include <mqueue.h>
#include <signal.h> /* Definition of SIGEV_* constants */
int mq_notify(mqd_t mqdes, const struct sigevent *sevp);
π DESCRIPTION π
okay so here's the vibe: mq_notify() lets the calling process register or unregister itself for delivery of an asynchronous notification when a new message shows up on the empty message queue that mqdes points at. It's giving "text me when something arrives" energy. π²
The sevp argument is a pointer to a sigevent structure. For the full definition and the general deets on that struct, go look at sigevent(3type) β she covers it all.
If sevp is a non-null pointer, then mq_notify() registers the calling process to receive message notification. The sigev_notify field of the sigevent structure that sevp points to says how the notification should be performed. That field gets one of these values, and bestie, there are three:
SIGEV_NONEβ a "null" notification. The calling process does get registered as the target for notification, but lowkey nothing happens when a message arrives: no notification is sent. It's the RSVP-but-never-show of notifications. πSIGEV_SIGNALβ notify the process by sending the signal specified insigev_signo. Seesigevent(3type)for the general details. Thesi_codefield of thesiginfo_tstructure is set toSI_MESGQ. Also (and this part is important),si_pidis set to the PID of the process that sent the message, andsi_uidis set to the real user ID of the sending process. Receipts fr fr. π«ΆSIGEV_THREADβ upon message delivery, invokesigev_notify_functionas if it were the start function of a new thread. Seesigevent(3type)for details. New thread just for you, main character energy only. π
Only one process can be registered to receive notification from a message queue. One. That's it. No group chats allowed. π ββοΈ
If sevp is NULL, and the calling process is currently registered to receive notifications for this message queue, then the registration is removed β and after that, another process can register to receive a message notification for this queue. Slots open up, bestie. ποΈ
Now for the timing rules, and these are the ones people get wrong:
- Message notification occurs only when a new message arrives and the queue was previously empty. If the queue wasn't empty at the time
mq_notify()was called, then a notification will occur only after the queue is emptied and a new message arrives. Past-tense messages don't count. β³ - If another process or thread is waiting to read a message from an empty queue using
mq_receive(3), then any message notification registration is ignored: the message is delivered to the process or thread callingmq_receive(3), and the message notification registration remains in effect. So your registration isn't cancelled, it's just benched for that round. πͺ - 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 wants the next notification, it can call
mq_notify()again to request a further notification. And here's the pro tip straight from the source: this should be done before emptying all unread messages from the queue. (Placing the queue in nonblocking mode is useful for emptying the queue of messages without blocking once it's empty.) Re-register first, then drain β that's the order, don't fight me on it. π§
π₯ RETURN VALUE π₯
On success, mq_notify() returns 0. On error, -1 is returned, and errno is set to indicate the error. Classic. β¨
π ERRORS π
EBADFβ The message queue descriptor specified inmqdesis invalid.EBUSYβ Another process has already registered to receive notification for this message queue. Somebody beat you to it, sorry babe. πEINVALβsevp->sigev_notifyis not one of the permitted values; orsevp->sigev_notifyisSIGEV_SIGNALandsevp->sigev_signois not a valid signal number. Something in there just doesn't parse.ENOMEMβ Insufficient memory.
Heads up bestie: POSIX.1-2008 says that an implementation may generate anEINVALerror ifsevpisNULL, and the caller is not currently registered to receive notifications for the queuemqdes. So don't count on it either way β it's a "maybe" and you should be cool with that. π
π¦ ATTRIBUTES π¦
For an explanation of the terms used in this section, see attributes(7).
- Interface:
mq_notify() - Attribute: Thread safety
- Value: MT-Safe β
So yeah, she's thread safe, she can run in the group chat without drama.
π VERSIONS π
π§΅ C library/kernel differences
In the glibc implementation, the mq_notify() library function is implemented on top of the system call of the same name. When sevp is NULL, or specifies a notification mechanism other than SIGEV_THREAD, the library function directly invokes the system call. Clean, one hop, no middleman. π
But for SIGEV_THREAD, much of the implementation lives within the library, rather than in the kernel. (This is necessarily so, since the thread involved in handling the notification is one that must be managed by the C library POSIX threads implementation.) And here's what that looks like under the hood: the implementation involves the use of a raw netlink(7) socket and creates a new thread for each notification that is delivered to the process. Thread per notification, every single time. Iconic and a little unhinged. π§΅β¨
π STANDARDS π
POSIX.1-2008.
π HISTORY π
POSIX.1-2001.
π EXAMPLES π
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. Very demure, very mindful: one message, one thread, then exit. π
π Program source π
#include <err.h>
#include <mqueue.h>
#include <pthread.h>
#include <signal.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 max. msg size; allocate buffer to receive msg */
if (mq_getattr(mqdes, &attr) == -1)
err(EXIT_FAILURE, "mq_getattr");
buf = malloc(attr.mq_msgsize);
if (buf == NULL)
err(EXIT_FAILURE, "malloc");
nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
if (nr == -1)
err(EXIT_FAILURE, "mq_receive");
printf("Read %zd bytes from MQ\n", nr);
free(buf);
exit(EXIT_SUCCESS); /* Terminate the process */
}
int
main(int argc, char *argv[])
{
mqd_t mqdes;
struct sigevent sev;
if (argc != 2) {
fprintf(stderr, "Usage: %s <mq-name>\n", argv[0]);
exit(EXIT_FAILURE);
}
mqdes = mq_open(argv[1], O_RDONLY);
if (mqdes == (mqd_t) -1)
err(EXIT_FAILURE, "mq_open");
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = tfunc;
sev.sigev_notify_attributes = NULL;
sev.sigev_value.sival_ptr = &mqdes; /* Arg. to thread func. */
if (mq_notify(mqdes, &sev) == -1)
err(EXIT_FAILURE, "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(3type)
Go read the whole friend group, they all link up. π«Ά
okay that's the whole page bestie, you're officially certified in message queue notifications. go forth and mq_notify responsibly. π
This little love letter comes to you from Linux man-pages 6.20, dated 2026-02-08, and yes she ate. π β¨πΈ
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2006, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms