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