1 .\" Copyright 1993 Giorgio Ciucci <giorgio@crcc.it>
2 .\" and Copyright 2015 Bill Pemberton <wfp5p@worldbroken.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified Tue Oct 22 16:40:11 1996 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified Mon Jul 10 21:09:59 2000 by aeb
28 .\" Modified 1 Jun 2002, Michael Kerrisk <mtk.manpages@gmail.com>
29 .\" Language clean-ups.
30 .\" Enhanced and corrected information on msg_qbytes, MSGMNB and MSGMAX
31 .\" Added note on restart behavior of msgsnd() and msgrcv()
32 .\" Formatting clean-ups (argument and field names marked as .I
34 .\" Modified, 27 May 2004, Michael Kerrisk <mtk.manpages@gmail.com>
35 .\" Added notes on capability requirements
36 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" Language and formatting clean-ups
38 .\" Added notes on /proc files
40 .TH MSGOP 2 2020-04-11 "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 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
362 capability in the user namespace that governs its IPC namespace.
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
417 capability in the user namespace that governs its IPC namespace.
420 The address pointed to by
425 While the process was sleeping to receive a message,
426 the message queue was removed.
429 While the process was sleeping to receive a message,
430 the process caught a signal; see
439 .BR EINVAL " (since Linux 3.14)"
446 .BR EINVAL " (since Linux 3.14)"
457 and no message of the requested type existed on the message queue.
465 and the queue contains less than
469 .BR ENOSYS " (since Linux 3.8)"
473 and this kernel was configured without
474 .BR CONFIG_CHECKPOINT_RESTORE .
476 POSIX.1-2001, POSIX.1-2008, SVr4.
482 flags are Linux-specific;
483 their definitions can be obtained by defining the
485 .\" MSG_COPY since glibc 2.18
492 isn't required on Linux or by any version of POSIX.
494 some old implementations required the inclusion of these header files,
495 and the SVID also documented their inclusion.
496 Applications intended to be portable to such old systems may need
497 to include these header files.
498 .\" Like Linux, the FreeBSD man pages still document
499 .\" the inclusion of these header files.
503 argument is declared as \fIstruct msgbuf\ *\fP in
505 It is declared as \fIvoid\ *\fP
506 in glibc 2.2 and later, as required by SUSv2 and SUSv3.
508 The following limits on message queue resources affect the
513 Maximum size of a message text, in bytes (default value: 8192 bytes).
514 On Linux, this limit can be read and modified via
515 .IR /proc/sys/kernel/msgmax .
518 Maximum number of bytes that can be held in a message queue
519 (default value: 16384 bytes).
520 On Linux, this limit can be read and modified via
521 .IR /proc/sys/kernel/msgmnb .
523 (Linux: a process with the
526 can increase the size of a message queue beyond
533 The implementation has no intrinsic system-wide limits on the
534 number of message headers
536 and the number of bytes in the message pool
539 In Linux 3.13 and earlier,
546 and the message queue contained less than
548 messages, then the call would block until the next message is written
550 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
551 At that point, the call would return a copy of the message,
553 of whether that message was at the ordinal position
556 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
565 is a logical error (since these flags impose different interpretations on
567 In Linux 3.13 and earlier,
568 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
569 this error was not diagnosed by
572 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
575 The program below demonstrates the use of
580 The example program is first run with the \fB\-s\fP option to send a
581 message and then run again with the \fB\-r\fP option to receive a
584 The following shell session shows a sample run of the program:
588 .RB "$" " ./a.out \-s"
589 sent: a message at Wed Mar 4 16:25:45 2015
591 .RB "$" " ./a.out \-r"
592 message received: a message at Wed Mar 4 16:25:45 2015
604 #include <sys/types.h>
614 usage(char *prog_name, char *msg)
619 fprintf(stderr, "Usage: %s [options]\en", prog_name);
620 fprintf(stderr, "Options are:\en");
621 fprintf(stderr, "\-s send message using msgsnd()\en");
622 fprintf(stderr, "\-r read message using msgrcv()\en");
623 fprintf(stderr, "\-t message type (default is 1)\en");
624 fprintf(stderr, "\-k message queue key (default is 1234)\en");
629 send_msg(int qid, int msgtype)
637 snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
640 if (msgsnd(qid, (void *) &msg, sizeof(msg.mtext),
641 IPC_NOWAIT) == \-1) {
642 perror("msgsnd error");
645 printf("sent: %s\en", msg.mtext);
649 get_msg(int qid, int msgtype)
653 if (msgrcv(qid, (void *) &msg, sizeof(msg.mtext), msgtype,
654 MSG_NOERROR | IPC_NOWAIT) == \-1) {
655 if (errno != ENOMSG) {
659 printf("No message available for msgrcv()\en");
661 printf("message received: %s\en", msg.mtext);
665 main(int argc, char *argv[])
668 int mode = 0; /* 1 = send, 2 = receive */
672 while ((opt = getopt(argc, argv, "srt:k:")) != \-1) {
681 msgtype = atoi(optarg);
683 usage(argv[0], "\-t option must be greater than 0\en");
686 msgkey = atoi(optarg);
689 usage(argv[0], "Unrecognized option\en");
694 usage(argv[0], "must use either \-s or \-r option\en");
696 qid = msgget(msgkey, IPC_CREAT | 0666);
704 get_msg(qid, msgtype);
706 send_msg(qid, msgtype);
714 .BR capabilities (7),