2 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH mq_notify 3 (date) "Linux man-pages (unreleased)"
8 mq_notify \- register for notification when a message is available
11 .RI ( librt ", " \-lrt )
14 .B #include <mqueue.h>
15 .BR "#include <signal.h> " "/* Definition of SIGEV_* constants */"
17 .BI "int mq_notify(mqd_t " mqdes ", const struct sigevent *" sevp );
21 allows the calling process to register or unregister for delivery of
22 an asynchronous notification when a new message arrives on
23 the empty message queue referred to by the message queue descriptor
28 argument is a pointer to a
31 For the definition and general details of this structure, see
36 is a non-null pointer, then
38 registers the calling process to receive message notification.
45 points specifies how notification is to be performed.
46 This field has one of the following values:
49 A "null" notification: the calling process is registered as the target
50 for notification, but when a message arrives, no notification is sent.
51 .\" When is SIGEV_NONE useful?
54 Notify the process by sending the signal specified in
63 structure will be set to
66 .\" I don't know of other implementations that set
67 .\" si_pid and si_uid -- MTK
69 will be set to the PID of the process that sent the message, and
71 will be set to the real user ID of the sending process.
74 Upon message delivery, invoke
75 .I sigev_notify_function
76 as if it were the start function of a new thread.
81 Only one process can be registered to receive notification
86 is NULL, and the calling process is currently registered to receive
87 notifications for this message queue, then the registration is removed;
88 another process can then register to receive a message notification
91 Message notification occurs only when a new message arrives and
92 the queue was previously empty.
93 If the queue was not empty at the time
95 was called, then a notification will occur only after
96 the queue is emptied and a new message arrives.
98 If another process or thread is waiting to read a message
99 from an empty queue using
101 then any message notification registration is ignored:
102 the message is delivered to the process or thread calling
104 and the message notification registration remains in effect.
106 Notification occurs once: after a notification is delivered,
107 the notification registration is removed,
108 and another process can register for message notification.
109 If the notified process wishes to receive the next notification,
112 to request a further notification.
113 This should be done before emptying all unread messages from the queue.
114 (Placing the queue in nonblocking mode is useful for emptying
115 the queue of messages without blocking once it is empty.)
119 returns 0; on error, \-1 is returned, with
121 set to indicate the error.
125 The message queue descriptor specified in
130 Another process has already registered to receive notification
131 for this message queue.
134 .I sevp\->sigev_notify
135 is not one of the permitted values; or
136 .I sevp\->sigev_notify
140 .I sevp\->sigev_signo
141 is not a valid signal number.
146 POSIX.1-2008 says that an implementation
150 .\" Linux does not do this
153 is NULL, and the caller is not currently registered to receive
154 notifications for the queue
157 For an explanation of the terms used in this section, see
165 Interface Attribute Value
168 T} Thread safety MT-Safe
177 .SS C library/kernel differences
178 In the glibc implementation, the
180 library function is implemented on top of the system call of the same name.
183 is NULL, or specifies a notification mechanism other than
185 the library function directly invokes the system call.
188 much of the implementation resides within the library,
189 rather than the kernel.
190 (This is necessarily so,
191 since the thread involved in handling the notification is one
192 that must be managed by the C library POSIX threads implementation.)
193 The implementation involves the use of a raw
195 socket and creates a new thread for each notification that is
196 delivered to the process.
198 The following program registers a notification request for the
199 message queue named in its command-line argument.
200 Notification is performed by creating a thread.
201 The thread executes a function which reads one message from the
202 queue and then terminates the process.
204 .\" SRC BEGIN (mq_notify.c)
213 #define handle_error(msg) \e
214 do { perror(msg); exit(EXIT_FAILURE); } while (0)
216 static void /* Thread start function */
217 tfunc(union sigval sv)
222 mqd_t mqdes = *((mqd_t *) sv.sival_ptr);
224 /* Determine max. msg size; allocate buffer to receive msg */
226 if (mq_getattr(mqdes, &attr) == \-1)
227 handle_error("mq_getattr");
228 buf = malloc(attr.mq_msgsize);
230 handle_error("malloc");
232 nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
234 handle_error("mq_receive");
236 printf("Read %zd bytes from MQ\en", nr);
238 exit(EXIT_SUCCESS); /* Terminate the process */
242 main(int argc, char *argv[])
248 fprintf(stderr, "Usage: %s <mq\-name>\en", argv[0]);
252 mqdes = mq_open(argv[1], O_RDONLY);
253 if (mqdes == (mqd_t) \-1)
254 handle_error("mq_open");
256 sev.sigev_notify = SIGEV_THREAD;
257 sev.sigev_notify_function = tfunc;
258 sev.sigev_notify_attributes = NULL;
259 sev.sigev_value.sival_ptr = &mqdes; /* Arg. to thread func. */
260 if (mq_notify(mqdes, &sev) == \-1)
261 handle_error("mq_notify");
263 pause(); /* Process will be terminated by thread function */