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 2021-03-22 "Linux" "Linux Programmer's Manual"
42 msgrcv, msgsnd \- System V message queue operations
45 .B #include <sys/msg.h>
47 .BI "int msgsnd(int " msqid ", const void *" msgp ", size_t " msgsz \
50 .BI "ssize_t msgrcv(int " msqid ", void *" msgp ", size_t " msgsz \
59 system calls are used to send messages to,
60 and receive messages from, a System\ V message queue.
61 The calling process must have write permission on the message queue
62 in order to send a message, and read permission to receive a message.
66 argument is a pointer to a caller-defined structure
67 of the following general form:
72 long mtype; /* message type, must be > 0 */
73 char mtext[1]; /* message data */
80 field is an array (or other structure) whose size is specified by
82 a nonnegative integer value.
83 Messages of zero length (i.e., no
88 field must have a strictly positive integer value.
90 used by the receiving process for message selection
91 (see the description of
97 system call appends a copy of the message pointed to by
99 to the message queue whose identifier is specified
103 If sufficient space is available in the queue,
105 succeeds immediately.
106 The queue capacity is governed by the
108 field in the associated data structure for the message queue.
109 During queue creation this field is initialized to
111 bytes, but this limit can be modified using
113 A message queue is considered to be full if either of the following
116 Adding a new message to the queue would cause the total number of bytes
117 in the queue to exceed the queue's maximum size (the
121 Adding another message to the queue would cause the total number of messages
122 in the queue to exceed the queue's maximum size (the
125 This check is necessary to prevent an unlimited number of zero-length
126 messages being placed on the queue.
127 Although such messages contain no data,
128 they nevertheless consume (locked) kernel memory.
130 If insufficient space is available in the queue, then the default
133 is to block until space becomes available.
138 then the call instead fails with the error
143 call may also fail if:
145 the queue is removed,
146 in which case the system call fails with
152 a signal is caught, in which case the system call fails
159 is never automatically restarted after being interrupted by a
160 signal handler, regardless of the setting of the
162 flag when establishing a signal handler.)
164 Upon successful completion the message queue data structure is updated
168 is set to the process ID of the calling process.
174 is set to the current time.
178 system call removes a message from the queue specified by
180 and places it in the buffer
186 specifies the maximum size in bytes for the member
188 of the structure pointed to by the
191 If the message text has length greater than
193 then the behavior depends on whether
200 the message text will be truncated (and the truncated part will be
203 is not specified, then
204 the message isn't removed from the queue and
205 the system call fails returning \-1 with
217 argument specifies the type of message requested, as follows:
222 then the first message in the queue is read.
227 then the first message in the queue of type
234 the first message in the queue of type not equal to
241 then the first message in the queue with the lowest type less than or
242 equal to the absolute value of
248 argument is a bit mask constructed by ORing together zero or more
249 of the following flags:
252 Return immediately if no message of the requested type is in the queue.
253 The system call fails with
258 .BR MSG_COPY " (since Linux 3.8)"
259 .\" commit 4a674f34ba04a002244edaf891b5da7fc1473ae8
260 Nondestructively fetch a copy of the message at the ordinal position
261 in the queue specified by
263 (messages are considered to be numbered starting at 0).
265 This flag must be specified in conjunction with
267 with the result that, if there is no message available at the given position,
268 the call fails immediately with the error
270 Because they alter the meaning of
276 may not both be specified in
281 flag was added for the implementation of
282 the kernel checkpoint-restore facility and
283 is available only if the kernel was built with the
284 .B CONFIG_CHECKPOINT_RESTORE
291 to read the first message in the queue with message type that differs
296 To truncate the message text if longer than
300 If no message of the requested type is available and
304 the calling process is blocked until one of the following conditions occurs:
306 A message of the desired type is placed in the queue.
308 The message queue is removed from the system.
309 In this case, the system call fails with
314 The calling process catches a signal.
315 In this case, the system call fails with
320 is never automatically restarted after being interrupted by a
321 signal handler, regardless of the setting of the
323 flag when establishing a signal handler.)
325 Upon successful completion the message queue data structure is updated
329 is set to the process ID of the calling process.
335 is set to the current time.
342 returns the number of bytes actually copied into the
345 On failure, both functions return \-1, and set
347 to indicate the error.
350 can fail with the following errors:
353 The calling process does not have write permission on the message queue,
354 and does not have the
356 capability in the user namespace that governs its IPC namespace.
359 The message can't be sent due to the
361 limit for the queue and
367 The address pointed to by
372 The message queue was removed.
375 Sleeping on a full message queue condition, the process caught a signal.
380 value, or nonpositive
385 value (less than 0 or greater than the system value
389 The system does not have enough memory to make a copy of the
390 message pointed to by
394 can fail with the following errors:
397 The message text length is greater than
405 The calling process does not have read permission on the message queue,
406 and does not have the
408 capability in the user namespace that governs its IPC namespace.
411 The address pointed to by
416 While the process was sleeping to receive a message,
417 the message queue was removed.
420 While the process was sleeping to receive a message,
421 the process caught a signal; see
430 .BR EINVAL " (since Linux 3.14)"
437 .BR EINVAL " (since Linux 3.14)"
448 and no message of the requested type existed on the message queue.
456 and the queue contains less than
460 .BR ENOSYS " (since Linux 3.8)"
467 and this kernel was configured without
468 .BR CONFIG_CHECKPOINT_RESTORE .
470 POSIX.1-2001, POSIX.1-2008, SVr4.
476 flags are Linux-specific;
477 their definitions can be obtained by defining the
479 .\" MSG_COPY since glibc 2.18
484 argument is declared as \fIstruct msgbuf\ *\fP in
486 It is declared as \fIvoid\ *\fP
487 in glibc 2.2 and later, as required by SUSv2 and SUSv3.
489 The following limits on message queue resources affect the
494 Maximum size of a message text, in bytes (default value: 8192 bytes).
495 On Linux, this limit can be read and modified via
496 .IR /proc/sys/kernel/msgmax .
499 Maximum number of bytes that can be held in a message queue
500 (default value: 16384 bytes).
501 On Linux, this limit can be read and modified via
502 .IR /proc/sys/kernel/msgmnb .
504 (Linux: a process with the
507 can increase the size of a message queue beyond
514 The implementation has no intrinsic system-wide limits on the
515 number of message headers
517 and the number of bytes in the message pool
520 In Linux 3.13 and earlier,
527 and the message queue contained less than
529 messages, then the call would block until the next message is written
531 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
532 At that point, the call would return a copy of the message,
534 of whether that message was at the ordinal position
537 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
546 is a logical error (since these flags impose different interpretations on
548 In Linux 3.13 and earlier,
549 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
550 this error was not diagnosed by
553 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
556 The program below demonstrates the use of
561 The example program is first run with the \fB\-s\fP option to send a
562 message and then run again with the \fB\-r\fP option to receive a
565 The following shell session shows a sample run of the program:
569 .RB "$" " ./a.out \-s"
570 sent: a message at Wed Mar 4 16:25:45 2015
572 .RB "$" " ./a.out \-r"
573 message received: a message at Wed Mar 4 16:25:45 2015
585 #include <sys/types.h>
595 usage(char *prog_name, char *msg)
600 fprintf(stderr, "Usage: %s [options]\en", prog_name);
601 fprintf(stderr, "Options are:\en");
602 fprintf(stderr, "\-s send message using msgsnd()\en");
603 fprintf(stderr, "\-r read message using msgrcv()\en");
604 fprintf(stderr, "\-t message type (default is 1)\en");
605 fprintf(stderr, "\-k message queue key (default is 1234)\en");
610 send_msg(int qid, int msgtype)
618 snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
621 if (msgsnd(qid, &msg, sizeof(msg.mtext),
622 IPC_NOWAIT) == \-1) {
623 perror("msgsnd error");
626 printf("sent: %s\en", msg.mtext);
630 get_msg(int qid, int msgtype)
634 if (msgrcv(qid, &msg, sizeof(msg.mtext), msgtype,
635 MSG_NOERROR | IPC_NOWAIT) == \-1) {
636 if (errno != ENOMSG) {
640 printf("No message available for msgrcv()\en");
642 printf("message received: %s\en", msg.mtext);
646 main(int argc, char *argv[])
649 int mode = 0; /* 1 = send, 2 = receive */
653 while ((opt = getopt(argc, argv, "srt:k:")) != \-1) {
662 msgtype = atoi(optarg);
664 usage(argv[0], "\-t option must be greater than 0\en");
667 msgkey = atoi(optarg);
670 usage(argv[0], "Unrecognized option\en");
675 usage(argv[0], "must use either \-s or \-r option\en");
677 qid = msgget(msgkey, IPC_CREAT | 0666);
685 get_msg(qid, msgtype);
687 send_msg(qid, msgtype);
695 .BR capabilities (7),