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_OVERVIEW 7 2015-08-08 "Linux" "Linux Programmer's Manual"
28 mq_overview \- overview of POSIX message queues
30 POSIX message queues allow processes to exchange data in
32 This API is distinct from that provided by System V message queues
36 etc.), but provides similar functionality.
38 Message queues are created and opened using
40 this function returns a
41 .I message queue descriptor
43 which is used to refer to the open message queue in later calls.
44 Each message queue is identified by a name of the form
46 that is, a null-terminated string of up to
48 (i.e., 255) characters consisting of an initial slash,
49 followed by one or more characters, none of which are slashes.
50 Two processes can operate on the same queue by passing the same name to
53 Messages are transferred to and from a queue using
57 When a process has finished using the queue, it closes it using
59 and when the queue is no longer required, it can be deleted using
61 Queue attributes can be retrieved and (in some cases) modified using
65 A process can request asynchronous notification
66 of the arrival of a message on a previously empty queue using
69 A message queue descriptor is a reference to an
70 .I "open message queue description"
75 a child inherits copies of its parent's message queue descriptors,
76 and these descriptors refer to the same open message queue descriptions
77 as the corresponding descriptors in the parent.
78 Corresponding descriptors in the two processes share the flags
80 that are associated with the open message queue description.
82 Each message has an associated
84 and messages are always delivered to the receiving process
85 highest priority first.
86 Message priorities range from 0 (low) to
87 .I sysconf(_SC_MQ_PRIO_MAX)\ -\ 1
90 .I sysconf(_SC_MQ_PRIO_MAX)
91 returns 32768, but POSIX.1 requires only that
92 an implementation support at least priorities in the range 0 to 31;
93 some implementations provide only this range.
95 The remainder of this section describes some specific details
96 of the Linux implementation of POSIX message queues.
97 .SS Library interfaces and system calls
100 library interfaces listed above are implemented
101 on top of underlying system calls of the same name.
102 Deviations from this scheme are indicated in the following table:
107 Library interface System call
109 mq_getattr(3) mq_getsetattr(2)
110 mq_notify(3) mq_notify(2)
111 mq_open(3) mq_open(2)
112 mq_receive(3) mq_timedreceive(2)
113 mq_send(3) mq_timedsend(2)
114 mq_setattr(3) mq_getsetattr(2)
115 mq_timedreceive(3) mq_timedreceive(2)
116 mq_timedsend(3) mq_timedsend(2)
117 mq_unlink(3) mq_unlink(2)
121 POSIX message queues have been supported on Linux since kernel 2.6.6.
122 Glibc support has been provided since version 2.3.4.
123 .SS Kernel configuration
124 Support for POSIX message queues is configurable via the
125 .B CONFIG_POSIX_MQUEUE
126 kernel configuration option.
127 This option is enabled by default.
129 POSIX message queues have kernel persistence:
132 a message queue will exist until the system is shut down.
134 Programs using the POSIX message queue API must be compiled with
136 to link against the real-time library,
139 The following interfaces can be used to limit the amount of
140 kernel memory consumed by POSIX message queues and to set
141 the default attributes for new message queues:
143 .IR /proc/sys/fs/mqueue/msg_default " (since Linux 3.5)"
144 This file defines the value used for a new queue's
146 setting when the queue is created with a call to
150 is specified as NULL.
151 The default value for this file is 10.
152 The minimum and maximum are as for
153 .IR /proc/sys/fs/mqueue/msg_max .
154 A new queue's default
156 value will be the smaller of
160 Up until Linux 2.6.28, the default
163 from Linux 2.6.28 to Linux 3.4, the default was the value defined for the
167 .I /proc/sys/fs/mqueue/msg_max
168 This file can be used to view and change the ceiling value for the
169 maximum number of messages in a queue.
170 This value acts as a ceiling on the
174 The default value for
177 The minimum value is 1 (10 in kernels before 2.6.28).
182 limit is ignored for privileged processes
183 .RB ( CAP_SYS_RESOURCE ),
186 ceiling is nevertheless imposed.
190 has changed across kernel versions:
194 .IR "131072\ /\ sizeof(void\ *)"
197 .IR "(32768\ *\ sizeof(void\ *) / 4)"
200 .\" commit 5b5c4d1a1440e94994c73dddbad7be0676cd8b9a
204 .IR /proc/sys/fs/mqueue/msgsize_default " (since Linux 3.5)"
205 This file defines the value used for a new queue's
207 setting when the queue is created with a call to
211 is specified as NULL.
212 The default value for this file is 8192 (bytes).
213 The minimum and maximum are as for
214 .IR /proc/sys/fs/mqueue/msgsize_max .
219 a new queue's default
221 value is capped to the
224 Up until Linux 2.6.28, the default
227 from Linux 2.6.28 to Linux 3.4, the default was the value defined for the
231 .I /proc/sys/fs/mqueue/msgsize_max
232 This file can be used to view and change the ceiling on the
233 maximum message size.
234 This value acts as a ceiling on the
238 The default value for
241 The minimum value is 128 (8192 in kernels before 2.6.28).
244 has varied across kernel versions:
247 Before Linux 2.6.28, the upper limit is
250 From Linux 2.6.28 to 3.4, the limit is 1,048,576.
252 Since Linux 3.5, the limit is 16,777,216
253 .RB ( HARD_MSGSIZEMAX ).
258 limit is ignored for privileged process
259 .RB ( CAP_SYS_RESOURCE ),
260 but, since Linux 3.5, the
262 ceiling is enforced for privileged processes.
264 .I /proc/sys/fs/mqueue/queues_max
265 This file can be used to view and change the system-wide limit on the
266 number of message queues that can be created.
267 The default value for
270 No ceiling is imposed on the
272 limit; privileged processes
273 .RB ( CAP_SYS_RESOURCE )
274 can exceed the limit (but see BUGS).
278 resource limit, which places a limit on the amount of space
279 that can be consumed by all of the message queues
280 belonging to a process's real user ID, is described in
282 .SS Mounting the message queue filesystem
283 On Linux, message queues are created in a virtual filesystem.
284 (Other implementations may also provide such a feature,
285 but the details are likely to differ.)
286 This filesystem can be mounted (by the superuser) using the following
291 .RB "#" " mkdir /dev/mqueue"
292 .RB "#" " mount \-t mqueue none /dev/mqueue"
296 The sticky bit is automatically enabled on the mount directory.
298 After the filesystem has been mounted, the message queues on the system
299 can be viewed and manipulated using the commands usually used for files
305 The contents of each file in the directory consist of a single line
306 containing information about the queue:
310 .RB "$" " cat /dev/mqueue/mymq"
311 QSIZE:129 NOTIFY:2 SIGNO:0 NOTIFY_PID:8260
315 These fields are as follows:
318 Number of bytes of data in all messages in the queue.
321 If this is nonzero, then the process with this PID has used
323 to register for asynchronous message notification,
324 and the remaining fields describe how notification occurs.
337 Signal number to be used for
339 .SS Polling message queue descriptors
340 On Linux, a message queue descriptor is actually a file descriptor,
341 and can be monitored using
346 This is not portable.
348 For a discussion of the interaction of System V IPC objects and
352 System V message queues
356 etc.) are an older API for exchanging messages between processes.
357 POSIX message queues provide a better designed interface than
358 System V message queues;
359 on the other hand POSIX message queues are less widely available
360 (especially on older systems) than System V message queues.
362 Linux does not currently (2.6.26) support the use of access control
363 lists (ACLs) for POSIX message queues.
365 An example of the use of various message queue functions is shown in
368 In Linux versions 3.5 to 3.14, the kernel imposed a ceiling of 1024
369 .RB ( HARD_QUEUESMAX )
370 on the value to which the
372 limit could be raised,
373 and the ceiling was enforced even for privileged processes.
374 This ceiling value was removed in Linux 3.14,
375 and patches to stable kernels 3.5.x to 3.13.x also removed the ceiling.
378 .BR mq_getsetattr (2),