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 .TH MSGOP 2 2016-10-08 "Linux" "Linux Programmer's Manual"
41 msgrcv, msgsnd \- System V message queue operations
44 .B #include <sys/types.h>
45 .B #include <sys/ipc.h>
46 .B #include <sys/msg.h>
48 .BI "int msgsnd(int " msqid ", const void *" msgp ", size_t " msgsz \
51 .BI "ssize_t msgrcv(int " msqid ", void *" msgp ", size_t " msgsz \
60 system calls are used, respectively, to send messages to,
61 and receive messages from, a System\ V message queue.
62 The calling process must have write permission on the message queue
63 in order to send a message, and read permission to receive a message.
67 argument is a pointer to a caller-defined structure
68 of the following general form:
73 long mtype; /* message type, must be > 0 */
74 char mtext[1]; /* message data */
81 field is an array (or other structure) whose size is specified by
83 a nonnegative integer value.
84 Messages of zero length (i.e., no
89 field must have a strictly positive integer value.
91 used by the receiving process for message selection
92 (see the description of
98 system call appends a copy of the message pointed to by
100 to the message queue whose identifier is specified
104 If sufficient space is available in the queue,
106 succeeds immediately.
107 The queue capacity is governed by the
109 field in the associated data structure for the message queue.
110 During queue creation this field is initialized to
112 bytes, but this limit can be modified using
114 A message queue is considered to be full if either of the following
117 Adding a new message to the queue would cause the total number of bytes
118 in the queue to exceed the queue's maximum size (the
122 Adding another message to the queue would cause the total number of messages
123 in the queue to exceed the queue's maximum size (the
126 This check is necessary to prevent an unlimited number of zero-length
127 messages being placed on the queue.
128 Although such messages contain no data,
129 they nevertheless consume (locked) kernel memory.
131 If insufficient space is available in the queue, then the default
134 is to block until space becomes available.
139 then the call instead fails with the error
144 call may also fail if:
146 the queue is removed,
147 in which case the system call fails with
153 a signal is caught, in which case the system call fails
160 is never automatically restarted after being interrupted by a
161 signal handler, regardless of the setting of the
163 flag when establishing a signal handler.)
165 Upon successful completion the message queue data structure is updated
169 is set to the process ID of the calling process.
175 is set to the current time.
179 system call removes a message from the queue specified by
181 and places it in the buffer
187 specifies the maximum size in bytes for the member
189 of the structure pointed to by the
192 If the message text has length greater than
194 then the behavior depends on whether
201 the message text will be truncated (and the truncated part will be
204 is not specified, then
205 the message isn't removed from the queue and
206 the system call fails returning \-1 with
218 argument specifies the type of message requested, as follows:
223 then the first message in the queue is read.
228 then the first message in the queue of type
235 the first message in the queue of type not equal to
242 then the first message in the queue with the lowest type less than or
243 equal to the absolute value of
249 argument is a bit mask constructed by ORing together zero or more
250 of the following flags:
253 Return immediately if no message of the requested type is in the queue.
254 The system call fails with
259 .BR MSG_COPY " (since Linux 3.8)"
260 .\" commit 4a674f34ba04a002244edaf891b5da7fc1473ae8
261 Nondestructively fetch a copy of the message at the ordinal position
262 in the queue specified by
264 (messages are considered to be numbered starting at 0).
266 This flag must be specified in conjunction with
268 with the result that, if there is no message available at the given position,
269 the call fails immediately with the error
271 Because they alter the meaning of
277 may not both be specified in
282 flag was added for the implementation of
283 the kernel checkpoint-restore facility and
284 is available only if the kernel was built with the
285 .B CONFIG_CHECKPOINT_RESTORE
292 to read the first message in the queue with message type that differs
297 To truncate the message text if longer than
301 If no message of the requested type is available and
305 the calling process is blocked until one of the following conditions occurs:
307 A message of the desired type is placed in the queue.
309 The message queue is removed from the system.
310 In this case, the system call fails with
315 The calling process catches a signal.
316 In this case, the system call fails with
321 is never automatically restarted after being interrupted by a
322 signal handler, regardless of the setting of the
324 flag when establishing a signal handler.)
326 Upon successful completion the message queue data structure is updated
330 is set to the process ID of the calling process.
336 is set to the current time.
338 On failure both functions return \-1
341 indicating the error,
347 returns the number of bytes actually copied into the
355 will be set to one among the following values:
358 The calling process does not have write permission on the message queue,
359 and does not have the
364 The message can't be sent due to the
366 limit for the queue and
372 The address pointed to by
377 The message queue was removed.
380 Sleeping on a full message queue condition, the process caught a signal.
385 value, or nonpositive
390 value (less than 0 or greater than the system value
394 The system does not have enough memory to make a copy of the
395 message pointed to by
402 will be set to one among the following values:
405 The message text length is greater than
413 The calling process does not have read permission on the message queue,
414 and does not have the
416 capability in the user namespace that governs its IPC namespace.
419 The address pointed to by
424 While the process was sleeping to receive a message,
425 the message queue was removed.
428 While the process was sleeping to receive a message,
429 the process caught a signal; see
438 .BR EINVAL " (since Linux 3.14)"
445 .BR EINVAL " (since Linux 3.14)"
456 and no message of the requested type existed on the message queue.
464 and the queue contains less than
468 .BR ENOSYS " (since Linux 3.8)"
472 and this kernel was configured without
473 .BR CONFIG_CHECKPOINT_RESTORE .
475 POSIX.1-2001, POSIX.1-2008, SVr4.
481 flags are Linux-specific;
482 their definitions can be obtained by defining the
484 .\" MSG_COPY since glibc 2.18
491 isn't required on Linux or by any version of POSIX.
493 some old implementations required the inclusion of these header files,
494 and the SVID also documented their inclusion.
495 Applications intended to be portable to such old systems may need
496 to include these header files.
497 .\" Like Linux, the FreeBSD man pages still document
498 .\" the inclusion of these header files.
502 argument is declared as \fIstruct msgbuf\ *\fP in
504 It is declared as \fIvoid\ *\fP
505 in glibc 2.2 and later, as required by SUSv2 and SUSv3.
507 The following limits on message queue resources affect the
512 Maximum size of a message text, in bytes (default value: 8192 bytes).
513 On Linux, this limit can be read and modified via
514 .IR /proc/sys/kernel/msgmax .
517 Maximum number of bytes that can be held in a message queue
518 (default value: 16384 bytes).
519 On Linux, this limit can be read and modified via
520 .IR /proc/sys/kernel/msgmnb .
522 (Linux: a process with the
525 can increase the size of a message queue beyond
532 The implementation has no intrinsic system-wide limits on the
533 number of message headers
535 and the number of bytes in the message pool
538 In Linux 3.13 and earlier,
545 and the message queue contained less than
547 messages, then the call would block until the next message is written
549 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
550 At that point, the call would return a copy of the message,
552 of whether that message was at the ordinal position
555 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
564 is a logical error (since these flags impose different interpretations on
566 In Linux 3.13 and earlier,
567 .\" http://marc.info/?l=linux-kernel&m=139048542803605&w=2
568 this error was not diagnosed by
571 .\" commit 4f87dac386cc43d5525da7a939d4b4e7edbea22c
574 The program below demonstrates the use of
579 The example program is first run with the \fB\-s\fP option to send a
580 message and then run again with the \fB\-r\fP option to receive a
583 The following shell session shows a sample run of the program:
587 .RB "$" " ./a.out \-s"
588 sent: a message at Wed Mar 4 16:25:45 2015
590 .RB "$" " ./a.out \-r"
591 message received: a message at Wed Mar 4 16:25:45 2015
603 #include <sys/types.h>
613 usage(char *prog_name, char *msg)
618 fprintf(stderr, "Usage: %s [options]\\n", prog_name);
619 fprintf(stderr, "Options are:\\n");
620 fprintf(stderr, "\-s send message using msgsnd()\\n");
621 fprintf(stderr, "\-r read message using msgrcv()\\n");
622 fprintf(stderr, "\-t message type (default is 1)\\n");
623 fprintf(stderr, "\-k message queue key (default is 1234)\\n");
628 send_msg(int qid, int msgtype)
636 snprintf(msg.mtext, sizeof(msg.mtext), "a message at %s",
639 if (msgsnd(qid, (void *) &msg, sizeof(msg.mtext),
640 IPC_NOWAIT) == \-1) {
641 perror("msgsnd error");
644 printf("sent: %s\\n", msg.mtext);
648 get_msg(int qid, int msgtype)
652 if (msgrcv(qid, (void *) &msg, sizeof(msg.mtext), msgtype,
653 MSG_NOERROR | IPC_NOWAIT) == \-1) {
654 if (errno != ENOMSG) {
658 printf("No message available for msgrcv()\\n");
660 printf("message received: %s\\n", msg.mtext);
664 main(int argc, char *argv[])
667 int mode = 0; /* 1 = send, 2 = receive */
671 while ((opt = getopt(argc, argv, "srt:k:")) != \-1) {
680 msgtype = atoi(optarg);
682 usage(argv[0], "\-t option must be greater than 0\\n");
685 msgkey = atoi(optarg);
688 usage(argv[0], "Unrecognized option\\n");
693 usage(argv[0], "must use either \-s or \-r option\\n");
695 qid = msgget(msgkey, IPC_CREAT | 0666);
703 get_msg(qid, msgtype);
705 send_msg(qid, msgtype);
713 .BR capabilities (7),