MQ_NOTIFY(3POSIX)
β¨ MQ_NOTIFY(3POSIX) β¨
okay bestie, welcome to the POSIX Programmer's Manual π this one's mq_notify, from the <mqueue.h> era, and it's giving very "your queue texted you back" energy π¦
π¨ PROLOG π¨
so like, real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so pls consult the corresponding Linux manual page for the tea on Linux behavior, ok?), or the interface may literally not be implemented on Linux at all π. vibes vary by system, iykyk.
πΈ NAME πΈ
mq_notify β notify process that a message is available (REALTIME)
periodt. that's the whole job.
π SYNOPSIS π
#include <mqueue.h>
int mq_notify(mqd_t mqdes, const struct sigevent *notification);
π DESCRIPTION π
okay so here's the situation. If the argument notification is not NULL, this function shall register the calling process to be notified of message arrival at an empty message queue associated with the specified message queue descriptor, mqdes. The notification specified by the notification argument shall be sent to the process when the message queue transitions from empty to non-empty π. It's literally the queue going "omg bestie, something's here."
At any time, only one process may be registered for notification by a message queue. One. Singular. Main character only π. If the calling process or any other process has already registered for notification of message arrival at the specified message queue, subsequent attempts to register for that message queue shall fail.
If notification is NULL and the process is currently registered for notification by the specified message queue, the existing registration shall be removed. bye bye registration π«Ά
When the notification is sent to the registered process, its registration shall be removed. The message queue shall then be available for registration again β the spot opens back up, slay β¨
And here's a lil plot twist: 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. like, the thread already caught the message so the notification never even gets a turn. not me getting ghosted by the notification π
β RETURN VALUE β
Upon successful completion, the mq_notify() function shall return a value of 0 β iconic, we ate. Otherwise, the function shall return a value of -1 and set errno to indicate the error. sad girl hours π
π₯ ERRORS π₯
The mq_notify() function shall fail if:
EBADFβ Themqdesargument is not a valid message queue descriptor. bestie that's not a real queue πEBUSYβ A process is already registered for notification by the message queue. someone else is already in the group chat, you gotta wait π
The mq_notify() function may fail if:
EINVALβ Thenotificationargument isNULLand the process is currently not registered. you tried to un-register from something you were never registered to. embarrassing but it happens π
The following sections are informative. (translation: cute bonus content, not the actual rules π¦)
π§ 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. yes it really does exit the whole process, that's the bit π₯
#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 */
}
π APPLICATION USAGE π
None.
like, literally nothing. empty. ghost town. π«₯
π RATIONALE π
None.
also nothing! they said "no thoughts, head empty" and honestly? respect. β¨
π FUTURE DIRECTIONS π
None.
no plans, no vision board, just vibes. π¦
π SEE ALSO π
mq_open()mq_send()mq_receive()msgctl()msgget()msgrcv()msgsnd()
The Base Definitions volume of POSIX.1β2017, <mqueue.h>
go read those too, they're all connected, it's a whole cinematic universe π¬
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
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 .
okay that's the whole page bestie, go forth and get notified π and as a lil sign-off: this one's from IEEE/The Open Group, dated 2017, and it's MQ_NOTIFY(3POSIX) β no cap, that's the real one. mwah π
π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms