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
38 .\" FIXME . Add example programs to this page.
40 .TH MSGOP 2 2014-05-16 "Linux" "Linux Programmer's Manual"
42 msgrcv, msgsnd \- System V message queue operations
45 .B #include <sys/types.h>
46 .B #include <sys/ipc.h>
47 .B #include <sys/msg.h>
49 .BI "int msgsnd(int " msqid ", const void *" msgp ", size_t " msgsz \
52 .BI "ssize_t msgrcv(int " msqid ", void *" msgp ", size_t " msgsz \
61 system calls are used, respectively, to send messages to,
62 and receive messages from, a System\ V message queue.
63 The calling process must have write permission on the message queue
64 in order to send a message, and read permission to receive a message.
68 argument is a pointer to a caller-defined structure
69 of the following general form:
74 long mtype; /* message type, must be > 0 */
75 char mtext[1]; /* message data */
82 field is an array (or other structure) whose size is specified by
84 a nonnegative integer value.
85 Messages of zero length (i.e., no
90 field must have a strictly positive integer value.
92 used by the receiving process for message selection
93 (see the description of
99 system call appends a copy of the message pointed to by
101 to the message queue whose identifier is specified
105 If sufficient space is available in the queue,
107 succeeds immediately.
108 The queue capacity is governed by the
110 field in the associated data structure for the message queue.
111 During queue creation this field is initialized to
113 bytes, but this limit can be modified using
115 A message queue is considered to be full if either of the following
118 Adding a new message to the queue would cause the total number of bytes
119 in the queue to exceed the queue's maximum size (the
123 Adding another message to the queue would cause the total number of messages
124 in the queue to exceed the queue's maximum size (the
127 This check is necessary to prevent an unlimited number of zero-length
128 messages being placed on the queue.
129 Although such messages contain no data,
130 they nevertheless consume (locked) kernel memory.
132 If insufficient space is available in the queue, then the default
135 is to block until space becomes available.
140 then the call instead fails with the error
145 call may also fail if:
147 the queue is removed,
148 in which case the system call fails with
154 a signal is caught, in which case the system call fails
161 is never automatically restarted after being interrupted by a
162 signal handler, regardless of the setting of the
164 flag when establishing a signal handler.)
166 Upon successful completion the message queue data structure is updated
170 is set to the process ID of the calling process.
176 is set to the current time.
180 system call removes a message from the queue specified by
182 and places it in the buffer
188 specifies the maximum size in bytes for the member
190 of the structure pointed to by the
193 If the message text has length greater than
195 then the behavior depends on whether
202 the message text will be truncated (and the truncated part will be
205 is not specified, then
206 the message isn't removed from the queue and
207 the system call fails returning \-1 with
219 argument specifies the type of message requested, as follows:
224 then the first message in the queue is read.
229 then the first message in the queue of type
236 the first message in the queue of type not equal to
243 then the first message in the queue with the lowest type less than or
244 equal to the absolute value of
250 argument is a bit mask constructed by ORing together zero or more
251 of the following flags:
254 Return immediately if no message of the requested type is in the queue.
255 The system call fails with
260 .BR MSG_COPY " (since Linux 3.8)"
261 .\" commit 4a674f34ba04a002244edaf891b5da7fc1473ae8
262 Nondestructively fetch a copy of the message at the ordinal position
263 in the queue specified by
265 (messages are considered to be numbered starting at 0).
267 This flag must be specified in conjunction with
269 with the result that, if there is no message available at the given position,
270 the call fails immediately with the error
272 Because they alter the meaning of
278 may not both be specified in
283 flag was added for the implementation of
284 the kernel checkpoint-restore facility and
285 is available only if the kernel was built with the
286 .B CONFIG_CHECKPOINT_RESTORE
293 to read the first message in the queue with message type that differs
298 To truncate the message text if longer than
302 If no message of the requested type is available and
306 the calling process is blocked until one of the following conditions occurs:
308 A message of the desired type is placed in the queue.
310 The message queue is removed from the system.
311 In this case, the system call fails with
316 The calling process catches a signal.
317 In this case, the system call fails with
322 is never automatically restarted after being interrupted by a
323 signal handler, regardless of the setting of the
325 flag when establishing a signal handler.)
327 Upon successful completion the message queue data structure is updated
331 is set to the process ID of the calling process.
337 is set to the current time.
339 On failure both functions return \-1
342 indicating the error,
348 returns the number of bytes actually copied into the
356 will be set to one among the following values:
359 The calling process does not have write permission on the message queue,
360 and does not have the
365 The message can't be sent due to the
367 limit for the queue and
373 The address pointed to by
378 The message queue was removed.
381 Sleeping on a full message queue condition, the process caught a signal.
386 value, or nonpositive
391 value (less than 0 or greater than the system value
395 The system does not have enough memory to make a copy of the
396 message pointed to by
403 will be set to one among the following values:
406 The message text length is greater than
414 The calling process does not have read permission on the message queue,
415 and does not have the
420 No message was available in the queue and
426 The address pointed to by
431 While the process was sleeping to receive a message,
432 the message queue was removed.
435 While the process was sleeping to receive a message,
436 the process caught a signal; see
445 .BR EINVAL " (since Linux 3.14)"
452 .BR EINVAL " (since Linux 3.14)"
463 and no message of the requested type existed on the message queue.
471 and the queue contains less than
475 .BR ENOSYS " (since Linux 3.8)"
479 and this kernel was configured without
480 .BR CONFIG_CHECKPOINT_RESTORE .
488 flags are Linux-specific;
489 their definitions can be obtained by defining the
491 .\" MSG_COPY since glibc 2.18
498 isn't required on Linux or by any version of POSIX.
500 some old implementations required the inclusion of these header files,
501 and the SVID also documented their inclusion.
502 Applications intended to be portable to such old systems may need
503 to include these header files.
504 .\" Like Linux, the FreeBSD man pages still document
505 .\" the inclusion of these header files.
509 argument is declared as \fIstruct msgbuf\ *\fP with
510 libc4, libc5, glibc 2.0, glibc 2.1.
511 It is declared as \fIvoid\ *\fP
512 with glibc 2.2 and later, as required by SUSv2 and SUSv3.
514 The following limits on message queue resources affect the
519 Maximum size for a message text: 8192 bytes
520 (on Linux, this limit can be read and modified via
521 .IR /proc/sys/kernel/msgmax ).
524 Default maximum size in bytes of a message queue: 16384 bytes
525 (on Linux, this limit can be read and modified via
526 .IR /proc/sys/kernel/msgmnb ).
528 (Linux: a process with the
531 can increase the size of a message queue beyond
537 The implementation has no intrinsic system-wide limits on the
538 number of message headers
540 and the number of bytes in the message pool
543 In Linux 3.13 and earlier,
550 and the message queue contained less than
552 messages, then the call would block until the next message is written
554 .\" FIXME http://marc.info/?l=linux-kernel&m=139048542803605&w=2
555 At that point, the call would return a copy of the message,
557 of whether that message was at the ordinal position
560 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
569 is a logical error (since these flags impose different interpretations on
571 In Linux 3.13 and earlier,
572 .\" FIXME http://marc.info/?l=linux-kernel&m=139048542803605&w=2
573 this error was not diagnosed by
576 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
581 .BR capabilities (7),
585 This page is part of release 3.67 of the Linux
588 A description of the project,
589 information about reporting bugs,
590 and the latest version of this page,
592 \%http://www.kernel.org/doc/man\-pages/.