OSDN Git Service

a36162bc460c2631a79a7ce41c704480c2cef565
[linuxjm/LDP_man-pages.git] / original / man7 / mq_overview.7
1 '\" t
2 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
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.
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH MQ_OVERVIEW 7 2014-09-21 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 mq_overview \- overview of POSIX message queues
29 .SH DESCRIPTION
30 POSIX message queues allow processes to exchange data in
31 the form of messages.
32 This API is distinct from that provided by System V message queues
33 .RB ( msgget (2),
34 .BR msgsnd (2),
35 .BR msgrcv (2),
36 etc.), but provides similar functionality.
37
38 Message queues are created and opened using
39 .BR mq_open (3);
40 this function returns a
41 .I message queue descriptor
42 .RI ( mqd_t ),
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
45 .IR /somename ;
46 that is, a null-terminated string of up to
47 .BI NAME_MAX
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
51 .BR mq_open (3).
52
53 Messages are transferred to and from a queue using
54 .BR mq_send (3)
55 and
56 .BR mq_receive (3).
57 When a process has finished using the queue, it closes it using
58 .BR mq_close (3),
59 and when the queue is no longer required, it can be deleted using
60 .BR mq_unlink (3).
61 Queue attributes can be retrieved and (in some cases) modified using
62 .BR mq_getattr (3)
63 and
64 .BR mq_setattr (3).
65 A process can request asynchronous notification
66 of the arrival of a message on a previously empty queue using
67 .BR mq_notify (3).
68
69 A message queue descriptor is a reference to an
70 .I "open message queue description"
71 (cf.
72 .BR open (2)).
73 After a
74 .BR fork (2),
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
79 .RI ( mq_flags )
80 that are associated with the open message queue description.
81
82 Each message has an associated
83 .IR priority ,
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
88 (high).
89 On Linux,
90 .I sysconf(_SC_MQ_PRIO_MAX)
91 returns 32768, but POSIX.1-2001 requires only that
92 an implementation support at least priorities in the range 0 to 31;
93 some implementations provide only this range.
94 .PP
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
98 In most cases the
99 .BR mq_* ()
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:
103 .RS
104 .TS
105 lB lB
106 l l.
107 Library interface       System call
108 mq_close(3)     close(2)
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)
118 .TE
119 .RE
120 .SS Versions
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.
128 .SS Persistence
129 POSIX message queues have kernel persistence:
130 if not removed by
131 .BR mq_unlink (3),
132 a message queue will exist until the system is shut down.
133 .SS Linking
134 Programs using the POSIX message queue API must be compiled with
135 .I cc \-lrt
136 to link against the real-time library,
137 .IR librt .
138 .SS /proc interfaces
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:
142 .TP
143 .IR /proc/sys/fs/mqueue/msg_default " (since Linux 3.5)"
144 This file defines the value used for a new queue's
145 .I mq_maxmsg
146 setting when the queue is created with a call to
147 .BR mq_open (3)
148 where
149 .I attr
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
155 .I mq_maxmsg
156 value will be the smaller of
157 .IR msg_default
158 and
159 .IR msg_max .
160 Up until Linux 2.6.28, the default
161 .I mq_maxmsg
162 was 10;
163 from Linux 2.6.28 to Linux 3.4, the default was the value defined for the
164 .I msg_max
165 limit.
166 .TP
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
171 .I attr\->mq_maxmsg
172 argument given to
173 .BR mq_open (3).
174 The default value for
175 .I msg_max
176 is 10.
177 The minimum value is 1 (10 in kernels before 2.6.28).
178 The upper limit is
179 .BR HARD_MSGMAX .
180 The
181 .I msg_max
182 limit is ignored for privileged processes
183 .RB ( CAP_SYS_RESOURCE ),
184 but the
185 .BR HARD_MSGMAX
186 ceiling is nevertheless imposed.
187
188 The definition of
189 .BR HARD_MSGMAX
190 has changed across kernel versions:
191 .RS
192 .IP * 3
193 Up to Linux 2.6.32:
194 .IR "131072\ /\ sizeof(void\ *)"
195 .IP *
196 Linux 2.6.33 to 3.4:
197 .IR "(32768\ *\ sizeof(void\ *) / 4)"
198 .IP *
199 Since Linux 3.5:
200 .\" commit 5b5c4d1a1440e94994c73dddbad7be0676cd8b9a
201 65,536
202 .RE
203 .TP
204 .IR /proc/sys/fs/mqueue/msgsize_default " (since Linux 3.5)"
205 This file defines the value used for a new queue's
206 .I mq_msgsize
207 setting when the queue is created with a call to
208 .BR mq_open (3)
209 where
210 .I attr
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 .
215 If
216 .IR msgsize_default
217 exceeds
218 .IR msgsize_max ,
219 a new queue's default
220 .I mq_msgsize
221 value is capped to the
222 .I msgsize_max
223 limit.
224 Up until Linux 2.6.28, the default
225 .I mq_msgsize
226 was 8192;
227 from Linux 2.6.28 to Linux 3.4, the default was the value defined for the
228 .I msgsize_max
229 limit.
230 .TP
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
235 .I attr\->mq_msgsize
236 argument given to
237 .BR mq_open (3).
238 The default value for
239 .I msgsize_max
240 is 8192 bytes.
241 The minimum value is 128 (8192 in kernels before 2.6.28).
242 The upper limit for
243 .I msgsize_max
244 has varied across kernel versions:
245 .RS
246 .IP * 3
247 Before Linux 2.6.28, the upper limit is
248 .BR INT_MAX .
249 .IP *
250 From Linux 2.6.28 to 3.4, the limit is 1,048,576.
251 .IP *
252 Since Linux 3.5, the limit is 16,777,216
253 .RB ( HARD_MSGSIZEMAX ).
254 .RE
255 .IP
256 The
257 .I msgsize_max
258 limit is ignored for privileged process
259 .RB ( CAP_SYS_RESOURCE ),
260 but, since Linux 3.5, the
261 .BR HARD_MSGSIZEMAX
262 ceiling is enforced for privileged processes.
263 .TP
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
268 .I queues_max
269 is 256.
270 No ceiling is imposed on the
271 .I queues_max
272 limit; privileged processes
273 .RB ( CAP_SYS_RESOURCE )
274 can exceed the limit (but see BUGS).
275 .SS Resource limit
276 The
277 .B RLIMIT_MSGQUEUE
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
281 .BR getrlimit (2).
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
287 commands:
288 .in +4n
289 .nf
290
291 .RB "#" " mkdir /dev/mqueue"
292 .RB "#" " mount \-t mqueue none /dev/mqueue"
293
294 .fi
295 .in
296 The sticky bit is automatically enabled on the mount directory.
297
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
300 (e.g.,
301 .BR ls (1)
302 and
303 .BR rm (1)).
304
305 The contents of each file in the directory consist of a single line
306 containing information about the queue:
307 .in +4n
308 .nf
309
310 .RB "$" " cat /dev/mqueue/mymq"
311 QSIZE:129     NOTIFY:2    SIGNO:0    NOTIFY_PID:8260
312
313 .fi
314 .in
315 These fields are as follows:
316 .TP
317 .B QSIZE
318 Number of bytes of data in all messages in the queue.
319 .TP
320 .B NOTIFY_PID
321 If this is nonzero, then the process with this PID has used
322 .BR mq_notify (3)
323 to register for asynchronous message notification,
324 and the remaining fields describe how notification occurs.
325 .TP
326 .B NOTIFY
327 Notification method:
328 0 is
329 .BR SIGEV_SIGNAL ;
330 1 is
331 .BR SIGEV_NONE ;
332 and
333 2 is
334 .BR SIGEV_THREAD .
335 .TP
336 .B SIGNO
337 Signal number to be used for
338 .BR SIGEV_SIGNAL .
339 .SS Polling message queue descriptors
340 On Linux, a message queue descriptor is actually a file descriptor,
341 and can be monitored using
342 .BR select (2),
343 .BR poll (2),
344 or
345 .BR epoll (7).
346 This is not portable.
347 .SS IPC namespaces
348 For a discussion of the interaction of System V IPC objects and
349 IPC namespaces, see
350 .BR namespaces (7).
351 .SH CONFORMING TO
352 POSIX.1-2001.
353 .SH NOTES
354 System V message queues
355 .RB ( msgget (2),
356 .BR msgsnd (2),
357 .BR msgrcv (2),
358 etc.) are an older API for exchanging messages between processes.
359 POSIX message queues provide a better designed interface than
360 System V message queues;
361 on the other hand POSIX message queues are less widely available
362 (especially on older systems) than System V message queues.
363
364 Linux does not currently (2.6.26) support the use of access control
365 lists (ACLs) for POSIX message queues.
366 .SH EXAMPLE
367 An example of the use of various message queue functions is shown in
368 .BR mq_notify (3).
369 .SH BUGS
370 In Linux versions 3.5 to 3.14, the kernel imposed a ceiling of 1024
371 .RB ( HARD_QUEUESMAX )
372 on the value to which the
373 .I queues_max
374 limit could be raised,
375 and the ceiling was enforced even for privileged processes.
376 This ceiling value was removed in Linux 3.14,
377 and patches to stable kernels 3.5.x to 3.13.x also removed the ceiling.
378 .SH SEE ALSO
379 .BR getrlimit (2),
380 .BR mq_getsetattr (2),
381 .BR poll (2),
382 .BR select (2),
383 .BR mq_close (3),
384 .BR mq_getattr (3),
385 .BR mq_notify (3),
386 .BR mq_open (3),
387 .BR mq_receive (3),
388 .BR mq_send (3),
389 .BR mq_unlink (3),
390 .BR epoll (7),
391 .BR namespaces (7)
392 .SH COLOPHON
393 This page is part of release 3.78 of the Linux
394 .I man-pages
395 project.
396 A description of the project,
397 information about reporting bugs,
398 and the latest version of this page,
399 can be found at
400 \%http://www.kernel.org/doc/man\-pages/.