]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/mq_overview.7
proc.5: Add "sf" to VmFlags in /proc/[pid]/smaps
[thirdparty/man-pages.git] / man7 / mq_overview.7
CommitLineData
80a99f39 1'\" t
c11b1abf 2.\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
80a99f39 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
80a99f39
MK
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.
8.\"
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.
c13182ef 13.\"
80a99f39
MK
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
10d76543
MK
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
20.\" professionally.
c13182ef 21.\"
80a99f39
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
80a99f39 25.\"
867c9b34 26.TH MQ_OVERVIEW 7 2019-10-10 "Linux" "Linux Programmer's Manual"
80a99f39 27.SH NAME
f68512e9 28mq_overview \- overview of POSIX message queues
80a99f39 29.SH DESCRIPTION
c13182ef 30POSIX message queues allow processes to exchange data in
80a99f39 31the form of messages.
91091371
MK
32This API is distinct from that provided by System V message queues
33.RB ( msgget (2),
34.BR msgsnd (2),
35.BR msgrcv (2),
36etc.), but provides similar functionality.
a721e8b2 37.PP
80a99f39
MK
38Message queues are created and opened using
39.BR mq_open (3);
40this function returns a
41.I message queue descriptor
42.RI ( mqd_t ),
43which is used to refer to the open message queue in later calls.
c13182ef 44Each message queue is identified by a name of the form
da39f64c
MK
45.IR /somename ;
46that is, a null-terminated string of up to
bd838488
MK
47.BI NAME_MAX
48(i.e., 255) characters consisting of an initial slash,
da39f64c 49followed by one or more characters, none of which are slashes.
80a99f39 50Two processes can operate on the same queue by passing the same name to
63f6a20a 51.BR mq_open (3).
a721e8b2 52.PP
80a99f39
MK
53Messages are transferred to and from a queue using
54.BR mq_send (3)
55and
56.BR mq_receive (3).
57When a process has finished using the queue, it closes it using
c13182ef 58.BR mq_close (3),
80a99f39
MK
59and when the queue is no longer required, it can be deleted using
60.BR mq_unlink (3).
61Queue attributes can be retrieved and (in some cases) modified using
62.BR mq_getattr (3)
63and
64.BR mq_setattr (3).
c13182ef 65A process can request asynchronous notification
80a99f39
MK
66of the arrival of a message on a previously empty queue using
67.BR mq_notify (3).
a721e8b2 68.PP
80a99f39 69A message queue descriptor is a reference to an
0daa9e92 70.I "open message queue description"
404fb8d3 71(see
80a99f39 72.BR open (2)).
c13182ef 73After a
80a99f39
MK
74.BR fork (2),
75a child inherits copies of its parent's message queue descriptors,
c13182ef 76and these descriptors refer to the same open message queue descriptions
d9cb0d7d
MK
77as the corresponding message queue descriptors in the parent.
78Corresponding message queue descriptors in the two processes share the flags
80a99f39
MK
79.RI ( mq_flags )
80that are associated with the open message queue description.
a721e8b2 81.PP
c13182ef
MK
82Each message has an associated
83.IR priority ,
84and messages are always delivered to the receiving process
80a99f39 85highest priority first.
c13182ef 86Message priorities range from 0 (low) to
80a99f39
MK
87.I sysconf(_SC_MQ_PRIO_MAX)\ -\ 1
88(high).
89On Linux,
c13182ef 90.I sysconf(_SC_MQ_PRIO_MAX)
775aa6b8 91returns 32768, but POSIX.1 requires only that
f341304c
MK
92an implementation support at least priorities in the range 0 to 31;
93some implementations provide only this range.
8b215c30
MK
94.PP
95The remainder of this section describes some specific details
1ce284ec 96of the Linux implementation of POSIX message queues.
c4ac1bc4
MK
97.SS Library interfaces and system calls
98In most cases the
ec9330d1 99.BR mq_* ()
c4ac1bc4
MK
100library interfaces listed above are implemented
101on top of underlying system calls of the same name.
102Deviations from this scheme are indicated in the following table:
793c8d4f 103.RS
c4ac1bc4
MK
104.TS
105lB lB
106l l.
107Library interface System call
108mq_close(3) close(2)
109mq_getattr(3) mq_getsetattr(2)
12e86dbf 110mq_notify(3) mq_notify(2)
c4ac1bc4
MK
111mq_open(3) mq_open(2)
112mq_receive(3) mq_timedreceive(2)
113mq_send(3) mq_timedsend(2)
114mq_setattr(3) mq_getsetattr(2)
115mq_timedreceive(3) mq_timedreceive(2)
116mq_timedsend(3) mq_timedsend(2)
117mq_unlink(3) mq_unlink(2)
118.TE
793c8d4f 119.RE
80a99f39
MK
120.SS Versions
121POSIX message queues have been supported on Linux since kernel 2.6.6.
122Glibc support has been provided since version 2.3.4.
123.SS Kernel configuration
124Support for POSIX message queues is configurable via the
125.B CONFIG_POSIX_MQUEUE
c13182ef 126kernel configuration option.
80a99f39
MK
127This option is enabled by default.
128.SS Persistence
129POSIX message queues have kernel persistence:
130if not removed by
63f6a20a 131.BR mq_unlink (3),
80a99f39
MK
132a message queue will exist until the system is shut down.
133.SS Linking
134Programs using the POSIX message queue API must be compiled with
135.I cc \-lrt
136to link against the real-time library,
137.IR librt .
138.SS /proc interfaces
c13182ef 139The following interfaces can be used to limit the amount of
3b3d3564
MK
140kernel memory consumed by POSIX message queues and to set
141the default attributes for new message queues:
80a99f39 142.TP
8ebedd6c
MK
143.IR /proc/sys/fs/mqueue/msg_default " (since Linux 3.5)"
144This file defines the value used for a new queue's
145.I mq_maxmsg
6f9e0e57 146setting when the queue is created with a call to
8ebedd6c
MK
147.BR mq_open (3)
148where
149.I attr
150is specified as NULL.
151The default value for this file is 10.
152The minimum and maximum are as for
153.IR /proc/sys/fs/mqueue/msg_max .
fcd98227 154A new queue's default
8ebedd6c 155.I mq_maxmsg
fcd98227
MK
156value will be the smaller of
157.IR msg_default
158and
159.IR msg_max .
8ebedd6c
MK
160Up until Linux 2.6.28, the default
161.I mq_maxmsg
162was 10;
163from Linux 2.6.28 to Linux 3.4, the default was the value defined for the
164.I msg_max
165limit.
166.TP
80a99f39 167.I /proc/sys/fs/mqueue/msg_max
c13182ef 168This file can be used to view and change the ceiling value for the
80a99f39
MK
169maximum number of messages in a queue.
170This value acts as a ceiling on the
94e9d9fe 171.I attr\->mq_maxmsg
80a99f39
MK
172argument given to
173.BR mq_open (3).
5bbdbc7e 174The default value for
80a99f39 175.I msg_max
5bbdbc7e
MK
176is 10.
177The minimum value is 1 (10 in kernels before 2.6.28).
178The upper limit is
2d5cee6b 179.BR HARD_MSGMAX .
5bd24c36
MK
180The
181.I msg_max
182limit is ignored for privileged processes
80a99f39 183.RB ( CAP_SYS_RESOURCE ),
c7496b03 184but the
8ef5d0c5 185.BR HARD_MSGMAX
c7496b03 186ceiling is nevertheless imposed.
a721e8b2 187.IP
2d5cee6b
MK
188The definition of
189.BR HARD_MSGMAX
190has changed across kernel versions:
82f92a9e
MK
191.RS
192.IP * 3
193Up to Linux 2.6.32:
194.IR "131072\ /\ sizeof(void\ *)"
195.IP *
196Linux 2.6.33 to 3.4:
b130fda8 197.IR "(32768\ *\ sizeof(void\ *) / 4)"
82f92a9e
MK
198.IP *
199Since Linux 3.5:
2d5cee6b 200.\" commit 5b5c4d1a1440e94994c73dddbad7be0676cd8b9a
82f92a9e
MK
20165,536
202.RE
80a99f39 203.TP
247d9cfd
MK
204.IR /proc/sys/fs/mqueue/msgsize_default " (since Linux 3.5)"
205This file defines the value used for a new queue's
206.I mq_msgsize
6f9e0e57 207setting when the queue is created with a call to
247d9cfd
MK
208.BR mq_open (3)
209where
210.I attr
211is specified as NULL.
fcd98227 212The default value for this file is 8192 (bytes).
247d9cfd
MK
213The minimum and maximum are as for
214.IR /proc/sys/fs/mqueue/msgsize_max .
215If
216.IR msgsize_default
217exceeds
218.IR msgsize_max ,
219a new queue's default
220.I mq_msgsize
221value is capped to the
222.I msgsize_max
223limit.
224Up until Linux 2.6.28, the default
225.I mq_msgsize
226was 8192;
227from Linux 2.6.28 to Linux 3.4, the default was the value defined for the
228.I msgsize_max
229limit.
230.TP
80a99f39 231.I /proc/sys/fs/mqueue/msgsize_max
c13182ef 232This file can be used to view and change the ceiling on the
80a99f39
MK
233maximum message size.
234This value acts as a ceiling on the
94e9d9fe 235.I attr\->mq_msgsize
80a99f39
MK
236argument given to
237.BR mq_open (3).
5bbdbc7e
MK
238The default value for
239.I msgsize_max
240is 8192 bytes.
241The minimum value is 128 (8192 in kernels before 2.6.28).
242The upper limit for
80a99f39 243.I msgsize_max
81020547 244has varied across kernel versions:
94d6f75f
MK
245.RS
246.IP * 3
247Before Linux 2.6.28, the upper limit is
248.BR INT_MAX .
249.IP *
250From Linux 2.6.28 to 3.4, the limit is 1,048,576.
251.IP *
252Since Linux 3.5, the limit is 16,777,216
81020547 253.RB ( HARD_MSGSIZEMAX ).
94d6f75f
MK
254.RE
255.IP
81020547
MK
256The
257.I msgsize_max
258limit is ignored for privileged process
259.RB ( CAP_SYS_RESOURCE ),
6f9e0e57 260but, since Linux 3.5, the
81020547
MK
261.BR HARD_MSGSIZEMAX
262ceiling is enforced for privileged processes.
80a99f39
MK
263.TP
264.I /proc/sys/fs/mqueue/queues_max
c13182ef 265This file can be used to view and change the system-wide limit on the
80a99f39 266number of message queues that can be created.
80a99f39
MK
267The default value for
268.I queues_max
40502a28 269is 256.
fcd98227 270No ceiling is imposed on the
40502a28 271.I queues_max
fcd98227 272limit; privileged processes
1052e1fb 273.RB ( CAP_SYS_RESOURCE )
fcd98227 274can exceed the limit (but see BUGS).
80a99f39
MK
275.SS Resource limit
276The
0daa9e92 277.B RLIMIT_MSGQUEUE
c13182ef
MK
278resource limit, which places a limit on the amount of space
279that can be consumed by all of the message queues
280belonging to a process's real user ID, is described in
80a99f39 281.BR getrlimit (2).
9ee4a2b6
MK
282.SS Mounting the message queue filesystem
283On Linux, message queues are created in a virtual filesystem.
c13182ef 284(Other implementations may also provide such a feature,
80a99f39 285but the details are likely to differ.)
9ee4a2b6 286This filesystem can be mounted (by the superuser) using the following
06ae751a 287commands:
e646a1ba 288.PP
a08ea57c 289.in +4n
e646a1ba 290.EX
06ae751a
MK
291.RB "#" " mkdir /dev/mqueue"
292.RB "#" " mount \-t mqueue none /dev/mqueue"
e646a1ba 293.EE
a08ea57c 294.in
e646a1ba 295.PP
80a99f39 296The sticky bit is automatically enabled on the mount directory.
a721e8b2 297.PP
9ee4a2b6 298After the filesystem has been mounted, the message queues on the system
80a99f39
MK
299can be viewed and manipulated using the commands usually used for files
300(e.g.,
301.BR ls (1)
302and
303.BR rm (1)).
a721e8b2 304.PP
c13182ef 305The contents of each file in the directory consist of a single line
80a99f39 306containing information about the queue:
e646a1ba 307.PP
a08ea57c 308.in +4n
e646a1ba 309.EX
8339b8bc 310.RB "$" " cat /dev/mqueue/mymq"
80a99f39 311QSIZE:129 NOTIFY:2 SIGNO:0 NOTIFY_PID:8260
e646a1ba 312.EE
a08ea57c 313.in
e646a1ba 314.PP
80a99f39
MK
315These fields are as follows:
316.TP
2fadbfb5 317.B QSIZE
a58feaee 318Number of bytes of data in all messages in the queue (but see BUGS).
80a99f39
MK
319.TP
320.B NOTIFY_PID
c7094399 321If this is nonzero, then the process with this PID has used
80a99f39 322.BR mq_notify (3)
c13182ef 323to register for asynchronous message notification,
80a99f39
MK
324and the remaining fields describe how notification occurs.
325.TP
326.B NOTIFY
327Notification method:
c13182ef 3280 is
80a99f39
MK
329.BR SIGEV_SIGNAL ;
3301 is
4df883b9 331.BR SIGEV_NONE ;
c13182ef 332and
80a99f39
MK
3332 is
334.BR SIGEV_THREAD .
335.TP
336.B SIGNO
337Signal number to be used for
338.BR SIGEV_SIGNAL .
785008cd
MK
339.SS Linux implementation of message queue descriptors
340On Linux, a message queue descriptor is actually a file descriptor.
341(POSIX does not require such an implementation.)
342This means that a message queue descriptor can be monitored using
80a99f39
MK
343.BR select (2),
344.BR poll (2),
345or
2315114c 346.BR epoll (7).
80a99f39 347This is not portable.
a721e8b2 348.PP
785008cd
MK
349The close-on-exec flag (see
350.BR open (2))
351is automatically set on the file descriptor returned by
352.BR mq_open (2).
1f1d2a8d 353.SS IPC namespaces
ab1dc749 354For a discussion of the interaction of POSIX message queue objects and
1f1d2a8d 355IPC namespaces, see
c4279d26 356.BR ipc_namespaces (7).
80a99f39
MK
357.SH NOTES
358System V message queues
359.RB ( msgget (2),
360.BR msgsnd (2),
361.BR msgrcv (2),
362etc.) are an older API for exchanging messages between processes.
363POSIX message queues provide a better designed interface than
c13182ef
MK
364System V message queues;
365on the other hand POSIX message queues are less widely available
80a99f39 366(especially on older systems) than System V message queues.
a721e8b2 367.PP
8f36e23f
MK
368Linux does not currently (2.6.26) support the use of access control
369lists (ACLs) for POSIX message queues.
fcd98227
MK
370.SH BUGS
371In Linux versions 3.5 to 3.14, the kernel imposed a ceiling of 1024
372.RB ( HARD_QUEUESMAX )
373on the value to which the
374.I queues_max
375limit could be raised,
376and the ceiling was enforced even for privileged processes.
377This ceiling value was removed in Linux 3.14,
378and patches to stable kernels 3.5.x to 3.13.x also removed the ceiling.
a721e8b2 379.PP
a58feaee
MK
380As originally implemented (and documented),
381the QSIZE field displayed the total number of (user-supplied)
382bytes in all messages in the message queue.
383Some changes in Linux 3.5
384.\" commit d6629859b36d
385inadvertently changed the behavior,
386so that this field also included a count of kernel overhead bytes
387used to store the messages in the queue.
388This behavioral regression was rectified in Linux 4.2
389.\" commit de54b9ac253787c366bbfb28d901a31954eb3511
390(and earlier stable kernel series),
391so that the count once more included just the bytes of user data
392in messages in the queue.
ba4add12
MK
393.SH EXAMPLE
394An example of the use of various message queue functions is shown in
395.BR mq_notify (3).
47297adb 396.SH SEE ALSO
80a99f39 397.BR getrlimit (2),
694ae673 398.BR mq_getsetattr (2),
f0c34053
MK
399.BR poll (2),
400.BR select (2),
80a99f39
MK
401.BR mq_close (3),
402.BR mq_getattr (3),
403.BR mq_notify (3),
404.BR mq_open (3),
405.BR mq_receive (3),
406.BR mq_send (3),
407.BR mq_unlink (3),
4effb5be
MK
408.BR epoll (7),
409.BR namespaces (7)