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. Two processes can operate on the
18 same queue by passing the same name to mq_open().
19
20 Messages are transferred to and from a queue using mq_send(3) and
21 mq_receive(3). When a process has finished using the queue, it closes
22 it using mq_close(3), and when the queue is no longer required, it can
23 be deleted using mq_unlink(3). Queue attributes can be retrieved and
24 (in some cases) modified using mq_getattr(3) and mq_setattr(3). A
25 process can request asynchronous notification of the arrival of a mes‐
26 sage on a previously empty queue using mq_notify(3).
27
28 A message queue descriptor is a reference to an open message queue
29 description (cf. open(2)). After a fork(2), a child inherits copies
30 of its parent's message queue descriptors, and these descriptors refer
31 to the same open message queue descriptions as the corresponding
32 descriptors in the parent. Corresponding descriptors in the two pro‐
33 cesses share the flags (mq_flags) that are associated with the open
34 message queue description.
35
36 Each message has an associated priority, and messages are always deliv‐
37 ered to the receiving process highest priority first. Message priori‐
38 ties range from 0 (low) to sysconf(_SC_MQ_PRIO_MAX) - 1 (high). On
39 Linux, sysconf(_SC_MQ_PRIO_MAX) returns 32768, but POSIX.1-2001 only
40 requires an implementation to support priorities in the range 0 to 31;
41 some implementations only provide this range.
42
43 Library interfaces and system calls
44 In most cases the mq_*() library interfaces listed above are imple‐
45 mented on top of underlying system calls of the same name. Deviations
46 from this scheme are indicated in the following table:
47
48 Library interface System call
49 mq_close(3) close(2)
50 mq_getattr(3) mq_getsetattr(2)
51 mq_open(3) mq_open(2)
52 mq_receive(3) mq_timedreceive(2)
53 mq_send(3) mq_timedsend(2)
54 mq_setattr(3) mq_getsetattr(2)
55 mq_timedreceive(3) mq_timedreceive(2)
56 mq_timedsend(3) mq_timedsend(2)
57 mq_unlink(3) mq_unlink(2)
58
60 Versions
61 POSIX message queues have been supported on Linux since kernel 2.6.6.
62 Glibc support has been provided since version 2.3.4.
63
64 Kernel configuration
65 Support for POSIX message queues is configurable via the CON‐
66 FIG_POSIX_MQUEUE kernel configuration option. This option is enabled
67 by default.
68
69 Persistence
70 POSIX message queues have kernel persistence: if not removed by
71 mq_unlink(), a message queue will exist until the system is shut down.
72
73 Linking
74 Programs using the POSIX message queue API must be compiled with cc
75 -lrt to link against the real-time library, librt.
76
77 /proc interfaces
78 The following interfaces can be used to limit the amount of kernel mem‐
79 ory consumed by POSIX message queues:
80
81 /proc/sys/fs/mqueue/msg_max
82 This file can be used to view and change the ceiling value for
83 the maximum number of messages in a queue. This value acts as a
84 ceiling on the attr->mq_maxmsg argument given to mq_open(3).
85 The default and minimum value for msg_max is 10; the upper limit
86 is HARD_MAX: (131072 / sizeof(void *)) (32768 on Linux/86).
87 This limit is ignored for privileged processes
88 (CAP_SYS_RESOURCE), but the HARD_MAX ceiling is nevertheless
89 imposed.
90
91 /proc/sys/fs/mqueue/msgsize_max
92 This file can be used to view and change the ceiling on the max‐
93 imum message size. This value acts as a ceiling on the
94 attr->mq_msgsize argument given to mq_open(3). The default and
95 minimum value for msgsize_max is 8192 bytes; the upper limit is
96 INT_MAX (2147483647 on Linux/86). This limit is ignored for
97 privileged processes (CAP_SYS_RESOURCE).
98
99 /proc/sys/fs/mqueue/queues_max
100 This file can be used to view and change the system-wide limit
101 on the number of message queues that can be created. Only priv‐
102 ileged processes (CAP_SYS_RESOURCE) can create new message
103 queues once this limit has been reached. The default value for
104 queues_max is 256; it can be changed to any value in the range 0
105 to INT_MAX.
106
107 Resource limit
108 The RLIMIT_MSGQUEUE resource limit, which places a limit on the amount
109 of space that can be consumed by all of the message queues belonging to
110 a process's real user ID, is described in getrlimit(2).
111
112 Mounting the message queue file system
113 On Linux, message queues are created in a virtual file system. (Other
114 implementations may also provide such a feature, but the details are
115 likely to differ.) This file system can be mounted using the following
116 commands:
117
118 $ mkdir /dev/mqueue
119 $ mount -t mqueue none /dev/mqueue
120
121 The sticky bit is automatically enabled on the mount directory.
122
123 After the file system has been mounted, the message queues on the sys‐
124 tem can be viewed and manipulated using the commands usually used for
125 files (e.g., ls(1) and rm(1)).
126
127 The contents of each file in the directory consist of a single line
128 containing information about the queue:
129
130 $ ls /dev/mqueue/mymq
131 QSIZE:129 NOTIFY:2 SIGNO:0 NOTIFY_PID:8260
132 $ mount -t mqueue none /dev/mqueue
133
134 These fields are as follows:
135
136 QSIZE Number of bytes of data in all messages in the queue.
137
138 NOTIFY_PID
139 If this is non-zero, then the process with this PID has used
140 mq_notify(3) to register for asynchronous message notification,
141 and the remaining fields describe how notification occurs.
142
143 NOTIFY Notification method: 0 is SIGEV_SIGNAL; 1 is SIGEV_NONE; and 2
144 is SIGEV_THREAD.
145
146 SIGNO Signal number to be used for SIGEV_SIGNAL.
147
148 Polling message queue descriptors
149 On Linux, a message queue descriptor is actually a file descriptor, and
150 can be monitored using select(2), poll(2), or epoll(7). This is not
151 portable.
152
154 POSIX.1-2001.
155
157 System V message queues (msgget(2), msgsnd(2), msgrcv(2), etc.) are an
158 older API for exchanging messages between processes. POSIX message
159 queues provide a better designed interface than System V message
160 queues; on the other hand POSIX message queues are less widely avail‐
161 able (especially on older systems) than System V message queues.
162
164 An example of the use of various message queue functions is shown in
165 mq_notify(3).
166
168 getrlimit(2), mq_getsetattr(2), mq_close(3), mq_getattr(3),
169 mq_notify(3), mq_open(3), mq_receive(3), mq_send(3), mq_unlink(3),
170 poll(2), select(2), epoll(4)
171
172
173
174Linux 2.6.16 2006-02-25 MQ_OVERVIEW(7)