1MQ_OVERVIEW(7) Linux Programmer's Manual MQ_OVERVIEW(7)
2
3
4
6 mq_overview - overview of POSIX message queues
7
9 POSIX message queues allow processes to exchange data in the form of
10 messages. This API is distinct from that provided by System V message
11 queues (msgget(2), msgsnd(2), msgrcv(2), etc.), but provides similar
12 functionality.
13
14 Message queues are created and opened using mq_open(3); this function
15 returns a message queue descriptor (mqd_t), which is used to refer to
16 the open message queue in later calls. Each message queue is identi‐
17 fied by a name of the form /somename; that is, a null-terminated string
18 of up to NAME_MAX (i.e., 255) characters consisting of an initial
19 slash, followed by one or more characters, none of which are slashes.
20 Two processes can operate on the same queue by passing the same name to
21 mq_open(3).
22
23 Messages are transferred to and from a queue using mq_send(3) and
24 mq_receive(3). When a process has finished using the queue, it closes
25 it using mq_close(3), and when the queue is no longer required, it can
26 be deleted using mq_unlink(3). Queue attributes can be retrieved and
27 (in some cases) modified using mq_getattr(3) and mq_setattr(3). A
28 process can request asynchronous notification of the arrival of a mes‐
29 sage on a previously empty queue using mq_notify(3).
30
31 A message queue descriptor is a reference to an open message queue
32 description (see open(2)). After a fork(2), a child inherits copies of
33 its parent's message queue descriptors, and these descriptors refer to
34 the same open message queue descriptions as the corresponding message
35 queue descriptors in the parent. Corresponding message queue descrip‐
36 tors in the two processes share the flags (mq_flags) that are associ‐
37 ated with the open message queue description.
38
39 Each message has an associated priority, and messages are always deliv‐
40 ered to the receiving process highest priority first. Message priori‐
41 ties range from 0 (low) to sysconf(_SC_MQ_PRIO_MAX) - 1 (high). On
42 Linux, sysconf(_SC_MQ_PRIO_MAX) returns 32768, but POSIX.1 requires
43 only that an implementation support at least priorities in the range 0
44 to 31; some implementations provide only this range.
45
46 The remainder of this section describes some specific details of the
47 Linux implementation of POSIX message queues.
48
49 Library interfaces and system calls
50 In most cases the mq_*() library interfaces listed above are imple‐
51 mented on top of underlying system calls of the same name. Deviations
52 from this scheme are indicated in the following table:
53
54 Library interface System call
55 mq_close(3) close(2)
56 mq_getattr(3) mq_getsetattr(2)
57 mq_notify(3) mq_notify(2)
58 mq_open(3) mq_open(2)
59 mq_receive(3) mq_timedreceive(2)
60 mq_send(3) mq_timedsend(2)
61 mq_setattr(3) mq_getsetattr(2)
62 mq_timedreceive(3) mq_timedreceive(2)
63 mq_timedsend(3) mq_timedsend(2)
64 mq_unlink(3) mq_unlink(2)
65
66 Versions
67 POSIX message queues have been supported on Linux since kernel 2.6.6.
68 Glibc support has been provided since version 2.3.4.
69
70 Kernel configuration
71 Support for POSIX message queues is configurable via the CON‐
72 FIG_POSIX_MQUEUE kernel configuration option. This option is enabled
73 by default.
74
75 Persistence
76 POSIX message queues have kernel persistence: if not removed by
77 mq_unlink(3), a message queue will exist until the system is shut down.
78
79 Linking
80 Programs using the POSIX message queue API must be compiled with cc
81 -lrt to link against the real-time library, librt.
82
83 /proc interfaces
84 The following interfaces can be used to limit the amount of kernel mem‐
85 ory consumed by POSIX message queues and to set the default attributes
86 for new message queues:
87
88 /proc/sys/fs/mqueue/msg_default (since Linux 3.5)
89 This file defines the value used for a new queue's mq_maxmsg
90 setting when the queue is created with a call to mq_open(3)
91 where attr is specified as NULL. The default value for this
92 file is 10. The minimum and maximum are as for
93 /proc/sys/fs/mqueue/msg_max. A new queue's default mq_maxmsg
94 value will be the smaller of msg_default and msg_max. Up until
95 Linux 2.6.28, the default mq_maxmsg was 10; from Linux 2.6.28 to
96 Linux 3.4, the default was the value defined for the msg_max
97 limit.
98
99 /proc/sys/fs/mqueue/msg_max
100 This file can be used to view and change the ceiling value for
101 the maximum number of messages in a queue. This value acts as a
102 ceiling on the attr->mq_maxmsg argument given to mq_open(3).
103 The default value for msg_max is 10. The minimum value is 1 (10
104 in kernels before 2.6.28). The upper limit is HARD_MSGMAX. The
105 msg_max limit is ignored for privileged processes
106 (CAP_SYS_RESOURCE), but the HARD_MSGMAX ceiling is nevertheless
107 imposed.
108
109 The definition of HARD_MSGMAX has changed across kernel ver‐
110 sions:
111
112 * Up to Linux 2.6.32: 131072 / sizeof(void *)
113
114 * Linux 2.6.33 to 3.4: (32768 * sizeof(void *) / 4)
115
116 * Since Linux 3.5: 65,536
117
118 /proc/sys/fs/mqueue/msgsize_default (since Linux 3.5)
119 This file defines the value used for a new queue's mq_msgsize
120 setting when the queue is created with a call to mq_open(3)
121 where attr is specified as NULL. The default value for this
122 file is 8192 (bytes). The minimum and maximum are as for
123 /proc/sys/fs/mqueue/msgsize_max. If msgsize_default exceeds
124 msgsize_max, a new queue's default mq_msgsize value is capped to
125 the msgsize_max limit. Up until Linux 2.6.28, the default
126 mq_msgsize was 8192; from Linux 2.6.28 to Linux 3.4, the default
127 was the value defined for the msgsize_max limit.
128
129 /proc/sys/fs/mqueue/msgsize_max
130 This file can be used to view and change the ceiling on the max‐
131 imum message size. This value acts as a ceiling on the
132 attr->mq_msgsize argument given to mq_open(3). The default
133 value for msgsize_max is 8192 bytes. The minimum value is 128
134 (8192 in kernels before 2.6.28). The upper limit for msg‐
135 size_max has varied across kernel versions:
136
137 * Before Linux 2.6.28, the upper limit is INT_MAX.
138
139 * From Linux 2.6.28 to 3.4, the limit is 1,048,576.
140
141 * Since Linux 3.5, the limit is 16,777,216 (HARD_MSGSIZEMAX).
142
143 The msgsize_max limit is ignored for privileged process
144 (CAP_SYS_RESOURCE), but, since Linux 3.5, the HARD_MSGSIZEMAX
145 ceiling is enforced for privileged processes.
146
147 /proc/sys/fs/mqueue/queues_max
148 This file can be used to view and change the system-wide limit
149 on the number of message queues that can be created. The
150 default value for queues_max is 256. No ceiling is imposed on
151 the queues_max limit; privileged processes (CAP_SYS_RESOURCE)
152 can exceed the limit (but see BUGS).
153
154 Resource limit
155 The RLIMIT_MSGQUEUE resource limit, which places a limit on the amount
156 of space that can be consumed by all of the message queues belonging to
157 a process's real user ID, is described in getrlimit(2).
158
159 Mounting the message queue filesystem
160 On Linux, message queues are created in a virtual filesystem. (Other
161 implementations may also provide such a feature, but the details are
162 likely to differ.) This filesystem can be mounted (by the superuser)
163 using the following commands:
164
165 # mkdir /dev/mqueue
166 # mount -t mqueue none /dev/mqueue
167
168 The sticky bit is automatically enabled on the mount directory.
169
170 After the filesystem has been mounted, the message queues on the system
171 can be viewed and manipulated using the commands usually used for files
172 (e.g., ls(1) and rm(1)).
173
174 The contents of each file in the directory consist of a single line
175 containing information about the queue:
176
177 $ cat /dev/mqueue/mymq
178 QSIZE:129 NOTIFY:2 SIGNO:0 NOTIFY_PID:8260
179
180 These fields are as follows:
181
182 QSIZE Number of bytes of data in all messages in the queue (but see
183 BUGS).
184
185 NOTIFY_PID
186 If this is nonzero, then the process with this PID has used
187 mq_notify(3) to register for asynchronous message notification,
188 and the remaining fields describe how notification occurs.
189
190 NOTIFY Notification method: 0 is SIGEV_SIGNAL; 1 is SIGEV_NONE; and 2
191 is SIGEV_THREAD.
192
193 SIGNO Signal number to be used for SIGEV_SIGNAL.
194
195 Linux implementation of message queue descriptors
196 On Linux, a message queue descriptor is actually a file descriptor.
197 (POSIX does not require such an implementation.) This means that a
198 message queue descriptor can be monitored using select(2), poll(2), or
199 epoll(7). This is not portable.
200
201 The close-on-exec flag (see open(2)) is automatically set on the file
202 descriptor returned by mq_open(2).
203
204 IPC namespaces
205 For a discussion of the interaction of POSIX message queue objects and
206 IPC namespaces, see ipc_namespaces(7).
207
209 System V message queues (msgget(2), msgsnd(2), msgrcv(2), etc.) are an
210 older API for exchanging messages between processes. POSIX message
211 queues provide a better designed interface than System V message
212 queues; on the other hand POSIX message queues are less widely avail‐
213 able (especially on older systems) than System V message queues.
214
215 Linux does not currently (2.6.26) support the use of access control
216 lists (ACLs) for POSIX message queues.
217
219 In Linux versions 3.5 to 3.14, the kernel imposed a ceiling of 1024
220 (HARD_QUEUESMAX) on the value to which the queues_max limit could be
221 raised, and the ceiling was enforced even for privileged processes.
222 This ceiling value was removed in Linux 3.14, and patches to stable
223 kernels 3.5.x to 3.13.x also removed the ceiling.
224
225 As originally implemented (and documented), the QSIZE field displayed
226 the total number of (user-supplied) bytes in all messages in the mes‐
227 sage queue. Some changes in Linux 3.5 inadvertently changed the behav‐
228 ior, so that this field also included a count of kernel overhead bytes
229 used to store the messages in the queue. This behavioral regression
230 was rectified in Linux 4.2 (and earlier stable kernel series), so that
231 the count once more included just the bytes of user data in messages in
232 the queue.
233
235 An example of the use of various message queue functions is shown in
236 mq_notify(3).
237
239 getrlimit(2), mq_getsetattr(2), poll(2), select(2), mq_close(3),
240 mq_getattr(3), mq_notify(3), mq_open(3), mq_receive(3), mq_send(3),
241 mq_unlink(3), epoll(7), namespaces(7)
242
244 This page is part of release 5.07 of the Linux man-pages project. A
245 description of the project, information about reporting bugs, and the
246 latest version of this page, can be found at
247 https://www.kernel.org/doc/man-pages/.
248
249
250
251Linux 2020-06-09 MQ_OVERVIEW(7)