OSDN Git Service

LDP: Update original to LDP v3.68
[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 2009-09-27 "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:
141 .TP
142 .I /proc/sys/fs/mqueue/msg_max
143 This file can be used to view and change the ceiling value for the
144 maximum number of messages in a queue.
145 This value acts as a ceiling on the
146 .I attr\->mq_maxmsg
147 argument given to
148 .BR mq_open (3).
149 The default value for
150 .I msg_max
151 is 10.
152 The minimum value is 1 (10 in kernels before 2.6.28).
153 The upper limit is
154 .BR HARD_MAX :
155 .IR "(131072\ /\ sizeof(void\ *))"
156 (32768 on Linux/86).
157 This limit is ignored for privileged processes
158 .RB ( CAP_SYS_RESOURCE ),
159 but the
160 .BR HARD_MAX
161 ceiling is nevertheless imposed.
162 .TP
163 .I /proc/sys/fs/mqueue/msgsize_max
164 This file can be used to view and change the ceiling on the
165 maximum message size.
166 This value acts as a ceiling on the
167 .I attr\->mq_msgsize
168 argument given to
169 .BR mq_open (3).
170 The default value for
171 .I msgsize_max
172 is 8192 bytes.
173 The minimum value is 128 (8192 in kernels before 2.6.28).
174 The upper limit for
175 .I msgsize_max
176 is 1,048,576 (in kernels before 2.6.28, the upper limit was
177 .BR INT_MAX ;
178 that is, 2,147,483,647 on Linux/86).
179 This limit is ignored for privileged processes
180 .RB ( CAP_SYS_RESOURCE ).
181 .TP
182 .I /proc/sys/fs/mqueue/queues_max
183 This file can be used to view and change the system-wide limit on the
184 number of message queues that can be created.
185 Only privileged processes
186 .RB ( CAP_SYS_RESOURCE )
187 can create new message queues once this limit has been reached.
188 The default value for
189 .I queues_max
190 is 256; it can be changed to any value in the range 0 to INT_MAX.
191 .SS Resource limit
192 The
193 .B RLIMIT_MSGQUEUE
194 resource limit, which places a limit on the amount of space
195 that can be consumed by all of the message queues
196 belonging to a process's real user ID, is described in
197 .BR getrlimit (2).
198 .SS Mounting the message queue filesystem
199 On Linux, message queues are created in a virtual filesystem.
200 (Other implementations may also provide such a feature,
201 but the details are likely to differ.)
202 This filesystem can be mounted (by the superuser) using the following
203 commands:
204 .in +4n
205 .nf
206
207 .RB "#" " mkdir /dev/mqueue"
208 .RB "#" " mount \-t mqueue none /dev/mqueue"
209
210 .fi
211 .in
212 The sticky bit is automatically enabled on the mount directory.
213
214 After the filesystem has been mounted, the message queues on the system
215 can be viewed and manipulated using the commands usually used for files
216 (e.g.,
217 .BR ls (1)
218 and
219 .BR rm (1)).
220
221 The contents of each file in the directory consist of a single line
222 containing information about the queue:
223 .in +4n
224 .nf
225
226 .RB "$" " cat /dev/mqueue/mymq"
227 QSIZE:129     NOTIFY:2    SIGNO:0    NOTIFY_PID:8260
228
229 .fi
230 .in
231 These fields are as follows:
232 .TP
233 .B QSIZE
234 Number of bytes of data in all messages in the queue.
235 .TP
236 .B NOTIFY_PID
237 If this is nonzero, then the process with this PID has used
238 .BR mq_notify (3)
239 to register for asynchronous message notification,
240 and the remaining fields describe how notification occurs.
241 .TP
242 .B NOTIFY
243 Notification method:
244 0 is
245 .BR SIGEV_SIGNAL ;
246 1 is
247 .BR SIGEV_NONE ;
248 and
249 2 is
250 .BR SIGEV_THREAD .
251 .TP
252 .B SIGNO
253 Signal number to be used for
254 .BR SIGEV_SIGNAL .
255 .SS Polling message queue descriptors
256 On Linux, a message queue descriptor is actually a file descriptor,
257 and can be monitored using
258 .BR select (2),
259 .BR poll (2),
260 or
261 .BR epoll (7).
262 This is not portable.
263 .SH CONFORMING TO
264 POSIX.1-2001.
265 .SH NOTES
266 System V message queues
267 .RB ( msgget (2),
268 .BR msgsnd (2),
269 .BR msgrcv (2),
270 etc.) are an older API for exchanging messages between processes.
271 POSIX message queues provide a better designed interface than
272 System V message queues;
273 on the other hand POSIX message queues are less widely available
274 (especially on older systems) than System V message queues.
275
276 Linux does not currently (2.6.26) support the use of access control
277 lists (ACLs) for POSIX message queues.
278 .SH EXAMPLE
279 An example of the use of various message queue functions is shown in
280 .BR mq_notify (3).
281 .SH SEE ALSO
282 .BR getrlimit (2),
283 .BR mq_getsetattr (2),
284 .BR poll (2),
285 .BR select (2),
286 .BR mq_close (3),
287 .BR mq_getattr (3),
288 .BR mq_notify (3),
289 .BR mq_open (3),
290 .BR mq_receive (3),
291 .BR mq_send (3),
292 .BR mq_unlink (3),
293 .BR epoll (7)
294 .SH COLOPHON
295 This page is part of release 3.68 of the Linux
296 .I man-pages
297 project.
298 A description of the project,
299 information about reporting bugs,
300 and the latest version of this page,
301 can be found at
302 \%http://www.kernel.org/doc/man\-pages/.