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 in sigev_signo. See sigevent(3type) for the general details. The si_code field of the siginfo_t structure is set to SI_MESGQ. Also (and this part is important), si_pid is set to the PID of the process that sent the message, and si_uid is set to the real user ID of the sending process. Receipts fr fr. 🫢
  • SIGEV_THREAD β€” upon message delivery, invoke sigev_notify_function as if it were the start function of a new thread. See sigevent(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 calling mq_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 in mqdes is invalid.
  • EBUSY β€” Another process has already registered to receive notification for this message queue. Somebody beat you to it, sorry babe. 😭
  • EINVAL β€” sevp->sigev_notify is not one of the permitted values; or sevp->sigev_notify is SIGEV_SIGNAL and sevp->sigev_signo is 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 an EINVAL error if sevp is NULL, and the caller is not currently registered to receive notifications for the queue mqdes. 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms