1 .\" Copyright 1993 Giorgio Ciucci <giorgio@crcc.it>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Modified Tue Oct 22 16:40:11 1996 by Eric S. Raymond <esr@thyrsus.com>
26 .\" Modified Mon Jul 10 21:09:59 2000 by aeb
27 .\" Modified 1 Jun 2002, Michael Kerrisk <mtk.manpages@gmail.com>
28 .\" Language clean-ups.
29 .\" Enhanced and corrected information on msg_qbytes, MSGMNB and MSGMAX
30 .\" Added note on restart behavior of msgsnd() and msgrcv()
31 .\" Formatting clean-ups (argument and field names marked as .I
33 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
34 .\" Added notes on capability requirements
35 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
36 .\" Language and formatting clean-ups
37 .\" Added notes on /proc files
39 .\" FIXME Add example programs to this page.
41 .TH MSGOP 2 2015-02-21 "Linux" "Linux Programmer's Manual"
43 msgrcv, msgsnd \- System V message queue operations
46 .B #include <sys/types.h>
47 .B #include <sys/ipc.h>
48 .B #include <sys/msg.h>
50 .BI "int msgsnd(int " msqid ", const void *" msgp ", size_t " msgsz \
53 .BI "ssize_t msgrcv(int " msqid ", void *" msgp ", size_t " msgsz \
62 system calls are used, respectively, to send messages to,
63 and receive messages from, a System\ V message queue.
64 The calling process must have write permission on the message queue
65 in order to send a message, and read permission to receive a message.
69 argument is a pointer to a caller-defined structure
70 of the following general form:
75 long mtype; /* message type, must be > 0 */
76 char mtext[1]; /* message data */
83 field is an array (or other structure) whose size is specified by
85 a nonnegative integer value.
86 Messages of zero length (i.e., no
91 field must have a strictly positive integer value.
93 used by the receiving process for message selection
94 (see the description of
100 system call appends a copy of the message pointed to by
102 to the message queue whose identifier is specified
106 If sufficient space is available in the queue,
108 succeeds immediately.
109 The queue capacity is governed by the
111 field in the associated data structure for the message queue.
112 During queue creation this field is initialized to
114 bytes, but this limit can be modified using
116 A message queue is considered to be full if either of the following
119 Adding a new message to the queue would cause the total number of bytes
120 in the queue to exceed the queue's maximum size (the
124 Adding another message to the queue would cause the total number of messages
125 in the queue to exceed the queue's maximum size (the
128 This check is necessary to prevent an unlimited number of zero-length
129 messages being placed on the queue.
130 Although such messages contain no data,
131 they nevertheless consume (locked) kernel memory.
133 If insufficient space is available in the queue, then the default
136 is to block until space becomes available.
141 then the call instead fails with the error
146 call may also fail if:
148 the queue is removed,
149 in which case the system call fails with
155 a signal is caught, in which case the system call fails
162 is never automatically restarted after being interrupted by a
163 signal handler, regardless of the setting of the
165 flag when establishing a signal handler.)
167 Upon successful completion the message queue data structure is updated
171 is set to the process ID of the calling process.
177 is set to the current time.
181 system call removes a message from the queue specified by
183 and places it in the buffer
189 specifies the maximum size in bytes for the member
191 of the structure pointed to by the
194 If the message text has length greater than
196 then the behavior depends on whether
203 the message text will be truncated (and the truncated part will be
206 is not specified, then
207 the message isn't removed from the queue and
208 the system call fails returning \-1 with
220 argument specifies the type of message requested, as follows:
225 then the first message in the queue is read.
230 then the first message in the queue of type
237 the first message in the queue of type not equal to
244 then the first message in the queue with the lowest type less than or
245 equal to the absolute value of
251 argument is a bit mask constructed by ORing together zero or more
252 of the following flags:
255 Return immediately if no message of the requested type is in the queue.
256 The system call fails with
261 .BR MSG_COPY " (since Linux 3.8)"
262 .\" commit 4a674f34ba04a002244edaf891b5da7fc1473ae8
263 Nondestructively fetch a copy of the message at the ordinal position
264 in the queue specified by
266 (messages are considered to be numbered starting at 0).
268 This flag must be specified in conjunction with
270 with the result that, if there is no message available at the given position,
271 the call fails immediately with the error
273 Because they alter the meaning of
279 may not both be specified in
284 flag was added for the implementation of
285 the kernel checkpoint-restore facility and
286 is available only if the kernel was built with the
287 .B CONFIG_CHECKPOINT_RESTORE
294 to read the first message in the queue with message type that differs
299 To truncate the message text if longer than
303 If no message of the requested type is available and
307 the calling process is blocked until one of the following conditions occurs:
309 A message of the desired type is placed in the queue.
311 The message queue is removed from the system.
312 In this case, the system call fails with
317 The calling process catches a signal.
318 In this case, the system call fails with
323 is never automatically restarted after being interrupted by a
324 signal handler, regardless of the setting of the
326 flag when establishing a signal handler.)
328 Upon successful completion the message queue data structure is updated
332 is set to the process ID of the calling process.
338 is set to the current time.
340 On failure both functions return \-1
343 indicating the error,
349 returns the number of bytes actually copied into the
357 will be set to one among the following values:
360 The calling process does not have write permission on the message queue,
361 and does not have the
366 The message can't be sent due to the
368 limit for the queue and
374 The address pointed to by
379 The message queue was removed.
382 Sleeping on a full message queue condition, the process caught a signal.
387 value, or nonpositive
392 value (less than 0 or greater than the system value
396 The system does not have enough memory to make a copy of the
397 message pointed to by
404 will be set to one among the following values:
407 The message text length is greater than
415 The calling process does not have read permission on the message queue,
416 and does not have the
421 No message was available in the queue and
427 The address pointed to by
432 While the process was sleeping to receive a message,
433 the message queue was removed.
436 While the process was sleeping to receive a message,
437 the process caught a signal; see
446 .BR EINVAL " (since Linux 3.14)"
453 .BR EINVAL " (since Linux 3.14)"
464 and no message of the requested type existed on the message queue.
472 and the queue contains less than
476 .BR ENOSYS " (since Linux 3.8)"
480 and this kernel was configured without
481 .BR CONFIG_CHECKPOINT_RESTORE .
489 flags are Linux-specific;
490 their definitions can be obtained by defining the
492 .\" MSG_COPY since glibc 2.18
499 isn't required on Linux or by any version of POSIX.
501 some old implementations required the inclusion of these header files,
502 and the SVID also documented their inclusion.
503 Applications intended to be portable to such old systems may need
504 to include these header files.
505 .\" Like Linux, the FreeBSD man pages still document
506 .\" the inclusion of these header files.
510 argument is declared as \fIstruct msgbuf\ *\fP in
512 It is declared as \fIvoid\ *\fP
513 in glibc 2.2 and later, as required by SUSv2 and SUSv3.
515 The following limits on message queue resources affect the
520 Maximum size of a message text, in bytes (default value: 8192 bytes).
521 On Linux, this limit can be read and modified via
522 .IR /proc/sys/kernel/msgmax .
525 Maximum number of bytes that can be held in a message queue
526 (default value: 16384 bytes).
527 On Linux, this limit can be read and modified via
528 .IR /proc/sys/kernel/msgmnb .
530 (Linux: a process with the
533 can increase the size of a message queue beyond
540 The implementation has no intrinsic system-wide limits on the
541 number of message headers
543 and the number of bytes in the message pool
546 In Linux 3.13 and earlier,
553 and the message queue contained less than
555 messages, then the call would block until the next message is written
557 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
558 At that point, the call would return a copy of the message,
560 of whether that message was at the ordinal position
563 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
572 is a logical error (since these flags impose different interpretations on
574 In Linux 3.13 and earlier,
575 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
576 this error was not diagnosed by
579 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
584 .BR capabilities (7),