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 de‐
32 scription (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 mq_un‐
77 link(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 (CAP_SYS_RE‐
106 SOURCE), but the HARD_MSGMAX ceiling is nevertheless imposed.
107
108 The definition of HARD_MSGMAX has changed across kernel ver‐
109 sions:
110
111 * Up to Linux 2.6.32: 131072 / sizeof(void *)
112
113 * Linux 2.6.33 to 3.4: (32768 * sizeof(void *) / 4)
114
115 * Since Linux 3.5: 65,536
116
117 /proc/sys/fs/mqueue/msgsize_default (since Linux 3.5)
118 This file defines the value used for a new queue's mq_msgsize
119 setting when the queue is created with a call to mq_open(3)
120 where attr is specified as NULL. The default value for this
121 file is 8192 (bytes). The minimum and maximum are as for
122 /proc/sys/fs/mqueue/msgsize_max. If msgsize_default exceeds ms‐
123 gsize_max, a new queue's default mq_msgsize value is capped to
124 the msgsize_max limit. Up until Linux 2.6.28, the default
125 mq_msgsize was 8192; from Linux 2.6.28 to Linux 3.4, the default
126 was the value defined for the msgsize_max limit.
127
128 /proc/sys/fs/mqueue/msgsize_max
129 This file can be used to view and change the ceiling on the max‐
130 imum message size. This value acts as a ceiling on the
131 attr->mq_msgsize argument given to mq_open(3). The default
132 value for msgsize_max is 8192 bytes. The minimum value is 128
133 (8192 in kernels before 2.6.28). The upper limit for msg‐
134 size_max has varied across kernel versions:
135
136 * Before Linux 2.6.28, the upper limit is INT_MAX.
137
138 * From Linux 2.6.28 to 3.4, the limit is 1,048,576.
139
140 * Since Linux 3.5, the limit is 16,777,216 (HARD_MSGSIZEMAX).
141
142 The msgsize_max limit is ignored for privileged process
143 (CAP_SYS_RESOURCE), but, since Linux 3.5, the HARD_MSGSIZEMAX
144 ceiling is enforced for privileged processes.
145
146 /proc/sys/fs/mqueue/queues_max
147 This file can be used to view and change the system-wide limit
148 on the number of message queues that can be created. The de‐
149 fault value for queues_max is 256. No ceiling is imposed on the
150 queues_max limit; privileged processes (CAP_SYS_RESOURCE) can
151 exceed the limit (but see BUGS).
152
153 Resource limit
154 The RLIMIT_MSGQUEUE resource limit, which places a limit on the amount
155 of space that can be consumed by all of the message queues belonging to
156 a process's real user ID, is described in getrlimit(2).
157
158 Mounting the message queue filesystem
159 On Linux, message queues are created in a virtual filesystem. (Other
160 implementations may also provide such a feature, but the details are
161 likely to differ.) This filesystem can be mounted (by the superuser)
162 using the following commands:
163
164 # mkdir /dev/mqueue
165 # mount -t mqueue none /dev/mqueue
166
167 The sticky bit is automatically enabled on the mount directory.
168
169 After the filesystem has been mounted, the message queues on the system
170 can be viewed and manipulated using the commands usually used for files
171 (e.g., ls(1) and rm(1)).
172
173 The contents of each file in the directory consist of a single line
174 containing information about the queue:
175
176 $ cat /dev/mqueue/mymq
177 QSIZE:129 NOTIFY:2 SIGNO:0 NOTIFY_PID:8260
178
179 These fields are as follows:
180
181 QSIZE Number of bytes of data in all messages in the queue (but see
182 BUGS).
183
184 NOTIFY_PID
185 If this is nonzero, then the process with this PID has used
186 mq_notify(3) to register for asynchronous message notification,
187 and the remaining fields describe how notification occurs.
188
189 NOTIFY Notification method: 0 is SIGEV_SIGNAL; 1 is SIGEV_NONE; and 2
190 is SIGEV_THREAD.
191
192 SIGNO Signal number to be used for SIGEV_SIGNAL.
193
194 Linux implementation of message queue descriptors
195 On Linux, a message queue descriptor is actually a file descriptor.
196 (POSIX does not require such an implementation.) This means that a
197 message queue descriptor can be monitored using select(2), poll(2), or
198 epoll(7). This is not portable.
199
200 The close-on-exec flag (see open(2)) is automatically set on the file
201 descriptor returned by mq_open(2).
202
203 IPC namespaces
204 For a discussion of the interaction of POSIX message queue objects and
205 IPC namespaces, see ipc_namespaces(7).
206
208 System V message queues (msgget(2), msgsnd(2), msgrcv(2), etc.) are an
209 older API for exchanging messages between processes. POSIX message
210 queues provide a better designed interface than System V message
211 queues; on the other hand POSIX message queues are less widely avail‐
212 able (especially on older systems) than System V message queues.
213
214 Linux does not currently (2.6.26) support the use of access control
215 lists (ACLs) for POSIX message queues.
216
218 In Linux versions 3.5 to 3.14, the kernel imposed a ceiling of 1024
219 (HARD_QUEUESMAX) on the value to which the queues_max limit could be
220 raised, and the ceiling was enforced even for privileged processes.
221 This ceiling value was removed in Linux 3.14, and patches to stable
222 kernels 3.5.x to 3.13.x also removed the ceiling.
223
224 As originally implemented (and documented), the QSIZE field displayed
225 the total number of (user-supplied) bytes in all messages in the mes‐
226 sage queue. Some changes in Linux 3.5 inadvertently changed the behav‐
227 ior, so that this field also included a count of kernel overhead bytes
228 used to store the messages in the queue. This behavioral regression
229 was rectified in Linux 4.2 (and earlier stable kernel series), so that
230 the count once more included just the bytes of user data in messages in
231 the queue.
232
234 An example of the use of various message queue functions is shown in
235 mq_notify(3).
236
238 getrlimit(2), mq_getsetattr(2), poll(2), select(2), mq_close(3),
239 mq_getattr(3), mq_notify(3), mq_open(3), mq_receive(3), mq_send(3),
240 mq_unlink(3), epoll(7), namespaces(7)
241
243 This page is part of release 5.13 of the Linux man-pages project. A
244 description of the project, information about reporting bugs, and the
245 latest version of this page, can be found at
246 https://www.kernel.org/doc/man-pages/.
247
248
249
250Linux 2020-06-09 MQ_OVERVIEW(7)