2 .\" Hey Emacs! This file is -*- nroff -*- source.
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH MQ_NOTIFY 3 2009-09-15 "Linux" "Linux Programmer's Manual"
28 mq_notify \- register for notification when a message is available
31 .B #include <mqueue.h>
33 .BI "mqd_t mq_notify(mqd_t " mqdes ", const struct sigevent *" notification );
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
48 structure that is defined something like the following:
52 union sigval { /* Data passed with notification */
53 int sival_int; /* Integer value */
54 void *sival_ptr; /* Pointer value */
58 int sigev_notify; /* Notification method */
59 int sigev_signo; /* Notification signal */
60 union sigval sigev_value; /* Data passed with
62 void (*sigev_notify_function) (union sigval);
63 /* Function for thread
65 void *sigev_notify_attributes;
66 /* Thread function attributes */
73 is a non-NULL pointer, then
75 registers the calling process to receive message notification.
82 points specifies how notification is to be performed.
83 This field has one of the following values:
86 A "null" notification: the calling process is registered as the target
87 for notification, but when a message arrives, no notification is sent.
88 .\" When is SIGEV_NONE useful?
91 Notify the process by sending the signal specified in
93 If the signal is caught with a signal handler that
94 was registered using the
97 flag, then the following fields are set in the
99 structure that is passed as the second argument of the handler:
104 is set to the signal number;
106 is set to the value specified in
107 .IR notification\->sigev_value ;
108 .\" I don't know of other implementations that set
109 .\" si_pid and si_uid -- MTK
111 is set to the PID of the process that sent the message; and
113 is set to the real user ID of the sending process.
114 The same information is available if the signal is accepted using
118 Deliver notification by invoking
119 .I notification\->sigev_notify_function
120 as the start function of a new thread.
121 The function is invoked with
122 .I notification\->sigev_value
123 as its sole argument.
125 .I notification\->sigev_notify_attributes
126 is not NULL, then it should point to a
128 structure that defines attributes for the thread (see
129 .BR pthread_attr_init (3)).
131 Only one process can be registered to receive notification
132 from a message queue.
136 is NULL, and the calling process is currently registered to receive
137 notifications for this message queue, then the registration is removed;
138 another process can then register to receive a message notification
141 Message notification only occurs when a new message arrives and
142 the queue was previously empty.
143 If the queue was not empty at the time
145 was called, then a notification will only occur after
146 the queue is emptied and a new message arrives.
148 If another process or thread is waiting to read a message
149 from an empty queue using
151 then any message notification registration is ignored:
152 the message is delivered to the process or thread calling
154 and the message notification registration remains in effect.
156 Notification occurs once: after a notification is delivered,
157 the notification registration is removed,
158 and another process can register for message notification.
159 If the notified process wishes to receive the next notification,
162 to request a further notification.
163 This should be done before emptying all unread messages from the queue.
164 (Placing the queue in nonblocking mode is useful for emptying
165 the queue of messages without blocking once it is empty.)
169 returns 0; on error, \-1 is returned, with
171 set to indicate the error.
175 The descriptor specified in
180 Another process has already registered to receive notification
181 for this message queue.
184 .I notification\->sigev_notify
185 is not one of the permitted values; or
186 .I notification\->sigev_notify
190 .I notification\->sigev_signo
191 is not a valid signal number.
196 POSIX.1-2008 says that an implementation
200 .\" Linux does not do this
203 is NULL, and the caller is not currently registered to receive
204 notifications for the queue
209 The following program registers a notification request for the
210 message queue named in its command-line argument.
211 Notification is performed by creating a thread.
212 The thread executes a function which reads one message from the
213 queue and then terminates the process.
222 #define handle_error(msg) \\
223 do { perror(msg); exit(EXIT_FAILURE); } while (0)
225 static void /* Thread start function */
226 tfunc(union sigval sv)
231 mqd_t mqdes = *((mqd_t *) sv.sival_ptr);
233 /* Determine max. msg size; allocate buffer to receive msg */
235 if (mq_getattr(mqdes, &attr) == \-1)
236 handle_error("mq_getattr");
237 buf = malloc(attr.mq_msgsize);
239 handle_error("malloc");
241 nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
243 handle_error("mq_receive");
245 printf("Read %ld bytes from MQ\\n", (long) nr);
247 exit(EXIT_SUCCESS); /* Terminate the process */
251 main(int argc, char *argv[])
257 fprintf(stderr, "Usage: %s <mq-name>\\n", argv[0]);
261 mqdes = mq_open(argv[1], O_RDONLY);
262 if (mqdes == (mqd_t) \-1)
263 handle_error("mq_open");
265 not.sigev_notify = SIGEV_THREAD;
266 not.sigev_notify_function = tfunc;
267 not.sigev_notify_attributes = NULL;
268 not.sigev_value.sival_ptr = &mqdes; /* Arg. to thread func. */
269 if (mq_notify(mqdes, ¬) == \-1)
270 handle_error("mq_notify");
272 pause(); /* Process will be terminated by thread function */