2 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH MQ_NOTIFY 3 2014-04-06 "Linux" "Linux Programmer's Manual"
28 mq_notify \- register for notification when a message is available
31 .B #include <mqueue.h>
33 .BI "int mq_notify(mqd_t " mqdes ", const struct sigevent *" sevp );
36 Link with \fI\-lrt\fP.
39 allows the calling process to register or unregister for delivery of
40 an asynchronous notification when a new message arrives on
41 the empty message queue referred to by the descriptor
46 argument is a pointer to a
49 For the definition and general details of this structure, see
54 is a non-null pointer, then
56 registers the calling process to receive message notification.
63 points specifies how notification is to be performed.
64 This field has one of the following values:
67 A "null" notification: the calling process is registered as the target
68 for notification, but when a message arrives, no notification is sent.
69 .\" When is SIGEV_NONE useful?
72 Notify the process by sending the signal specified in
81 structure will be set to
84 .\" I don't know of other implementations that set
85 .\" si_pid and si_uid -- MTK
87 will be set to the PID of the process that sent the message, and
89 will be set to the real user ID of the sending process.
92 Upon message delivery, invoke
93 .I sigev_notify_function
94 as if it were the start function of a new thread.
99 Only one process can be registered to receive notification
100 from a message queue.
104 is NULL, and the calling process is currently registered to receive
105 notifications for this message queue, then the registration is removed;
106 another process can then register to receive a message notification
109 Message notification occurs only when a new message arrives and
110 the queue was previously empty.
111 If the queue was not empty at the time
113 was called, then a notification will occur only after
114 the queue is emptied and a new message arrives.
116 If another process or thread is waiting to read a message
117 from an empty queue using
119 then any message notification registration is ignored:
120 the message is delivered to the process or thread calling
122 and the message notification registration remains in effect.
124 Notification occurs once: after a notification is delivered,
125 the notification registration is removed,
126 and another process can register for message notification.
127 If the notified process wishes to receive the next notification,
130 to request a further notification.
131 This should be done before emptying all unread messages from the queue.
132 (Placing the queue in nonblocking mode is useful for emptying
133 the queue of messages without blocking once it is empty.)
137 returns 0; on error, \-1 is returned, with
139 set to indicate the error.
143 The descriptor specified in
148 Another process has already registered to receive notification
149 for this message queue.
152 .I sevp\->sigev_notify
153 is not one of the permitted values; or
154 .I sevp\->sigev_notify
158 .I sevp\->sigev_signo
159 is not a valid signal number.
164 POSIX.1-2008 says that an implementation
168 .\" Linux does not do this
171 is NULL, and the caller is not currently registered to receive
172 notifications for the queue
177 The following program registers a notification request for the
178 message queue named in its command-line argument.
179 Notification is performed by creating a thread.
180 The thread executes a function which reads one message from the
181 queue and then terminates the process.
190 #define handle_error(msg) \\
191 do { perror(msg); exit(EXIT_FAILURE); } while (0)
193 static void /* Thread start function */
194 tfunc(union sigval sv)
199 mqd_t mqdes = *((mqd_t *) sv.sival_ptr);
201 /* Determine max. msg size; allocate buffer to receive msg */
203 if (mq_getattr(mqdes, &attr) == \-1)
204 handle_error("mq_getattr");
205 buf = malloc(attr.mq_msgsize);
207 handle_error("malloc");
209 nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
211 handle_error("mq_receive");
213 printf("Read %zd bytes from MQ\\n", nr);
215 exit(EXIT_SUCCESS); /* Terminate the process */
219 main(int argc, char *argv[])
225 fprintf(stderr, "Usage: %s <mq\-name>\\n", argv[0]);
229 mqdes = mq_open(argv[1], O_RDONLY);
230 if (mqdes == (mqd_t) \-1)
231 handle_error("mq_open");
233 sev.sigev_notify = SIGEV_THREAD;
234 sev.sigev_notify_function = tfunc;
235 sev.sigev_notify_attributes = NULL;
236 sev.sigev_value.sival_ptr = &mqdes; /* Arg. to thread func. */
237 if (mq_notify(mqdes, &sev) == \-1)
238 handle_error("mq_notify");
240 pause(); /* Process will be terminated by thread function */
253 This page is part of release 3.79 of the Linux
256 A description of the project,
257 information about reporting bugs,
258 and the latest version of this page,
260 \%http://www.kernel.org/doc/man\-pages/.