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 (cf. open(2)). After a fork(2), a child inherits copies
33 of its parent's message queue descriptors, and these descriptors refer
34 to the same open message queue descriptions as the corresponding
35 descriptors in the parent. Corresponding descriptors in the two pro‐
36 cesses share the flags (mq_flags) that are associated with the open
37 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-2001 only
43 requires an implementation to support priorities in the range 0 to 31;
44 some implementations only provide 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:
86
87 /proc/sys/fs/mqueue/msg_max
88 This file can be used to view and change the ceiling value for
89 the maximum number of messages in a queue. This value acts as a
90 ceiling on the attr->mq_maxmsg argument given to mq_open(3).
91 The default value for msg_max is 10. The minimum value is 1 (10
92 in kernels before 2.6.28). The upper limit is HARD_MAX:
93 (131072 / sizeof(void *)) (32768 on Linux/86). This limit is
94 ignored for privileged processes (CAP_SYS_RESOURCE), but the
95 HARD_MAX ceiling is nevertheless imposed.
96
97 /proc/sys/fs/mqueue/msgsize_max
98 This file can be used to view and change the ceiling on the max‐
99 imum message size. This value acts as a ceiling on the
100 attr->mq_msgsize argument given to mq_open(3). The default
101 value for msgsize_max is 8192 bytes. The minimum value is 128
102 (8192 in kernels before 2.6.28). The upper limit for msg‐
103 size_max is 1,048,576 (in kernels before 2.6.28, the upper limit
104 was INT_MAX; that is, 2,147,483,647 on Linux/86). This limit is
105 ignored for privileged processes (CAP_SYS_RESOURCE).
106
107 /proc/sys/fs/mqueue/queues_max
108 This file can be used to view and change the system-wide limit
109 on the number of message queues that can be created. Only priv‐
110 ileged processes (CAP_SYS_RESOURCE) can create new message
111 queues once this limit has been reached. The default value for
112 queues_max is 256; it can be changed to any value in the range 0
113 to INT_MAX.
114
115 Resource limit
116 The RLIMIT_MSGQUEUE resource limit, which places a limit on the amount
117 of space that can be consumed by all of the message queues belonging to
118 a process's real user ID, is described in getrlimit(2).
119
120 Mounting the message queue file system
121 On Linux, message queues are created in a virtual file system. (Other
122 implementations may also provide such a feature, but the details are
123 likely to differ.) This file system can be mounted (by the superuser)
124 using the following commands:
125
126 # mkdir /dev/mqueue
127 # mount -t mqueue none /dev/mqueue
128
129 The sticky bit is automatically enabled on the mount directory.
130
131 After the file system has been mounted, the message queues on the sys‐
132 tem can be viewed and manipulated using the commands usually used for
133 files (e.g., ls(1) and rm(1)).
134
135 The contents of each file in the directory consist of a single line
136 containing information about the queue:
137
138 $ cat /dev/mqueue/mymq
139 QSIZE:129 NOTIFY:2 SIGNO:0 NOTIFY_PID:8260
140
141 These fields are as follows:
142
143 QSIZE Number of bytes of data in all messages in the queue.
144
145 NOTIFY_PID
146 If this is nonzero, then the process with this PID has used
147 mq_notify(3) to register for asynchronous message notification,
148 and the remaining fields describe how notification occurs.
149
150 NOTIFY Notification method: 0 is SIGEV_SIGNAL; 1 is SIGEV_NONE; and 2
151 is SIGEV_THREAD.
152
153 SIGNO Signal number to be used for SIGEV_SIGNAL.
154
155 Polling message queue descriptors
156 On Linux, a message queue descriptor is actually a file descriptor, and
157 can be monitored using select(2), poll(2), or epoll(7). This is not
158 portable.
159
161 POSIX.1-2001.
162
164 System V message queues (msgget(2), msgsnd(2), msgrcv(2), etc.) are an
165 older API for exchanging messages between processes. POSIX message
166 queues provide a better designed interface than System V message
167 queues; on the other hand POSIX message queues are less widely avail‐
168 able (especially on older systems) than System V message queues.
169
170 Linux does not currently (2.6.26) support the use of access control
171 lists (ACLs) for POSIX message queues.
172
174 An example of the use of various message queue functions is shown in
175 mq_notify(3).
176
178 getrlimit(2), mq_getsetattr(2), poll(2), select(2), mq_close(3),
179 mq_getattr(3), mq_notify(3), mq_open(3), mq_receive(3), mq_send(3),
180 mq_unlink(3), epoll(7)
181
183 This page is part of release 3.25 of the Linux man-pages project. A
184 description of the project, and information about reporting bugs, can
185 be found at http://www.kernel.org/doc/man-pages/.
186
187
188
189Linux 2009-09-27 MQ_OVERVIEW(7)