1MSGRCV(3P) POSIX Programmer's Manual MSGRCV(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 msgrcv — XSI message receive operation
13
15 #include <sys/msg.h>
16
17 ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
18 int msgflg);
19
21 The msgrcv() function operates on XSI message queues (see the Base Def‐
22 initions volume of POSIX.1‐2017, Section 3.226, Message Queue). It is
23 unspecified whether this function interoperates with the realtime
24 interprocess communication facilities defined in Section 2.8, Realtime.
25
26 The msgrcv() function shall read a message from the queue associated
27 with the message queue identifier specified by msqid and place it in
28 the user-defined buffer pointed to by msgp.
29
30 The application shall ensure that the argument msgp points to a user-
31 defined buffer that contains first a field of type long specifying the
32 type of the message, and then a data portion that holds the data bytes
33 of the message. The structure below is an example of what this user-
34 defined buffer might look like:
35
36
37 struct mymsg {
38 long mtype; /* Message type. */
39 char mtext[1]; /* Message text. */
40 }
41
42 The structure member mtype is the received message's type as specified
43 by the sending process.
44
45 The structure member mtext is the text of the message.
46
47 The argument msgsz specifies the size in bytes of mtext. The received
48 message shall be truncated to msgsz bytes if it is larger than msgsz
49 and (msgflg & MSG_NOERROR) is non-zero. The truncated part of the mes‐
50 sage shall be lost and no indication of the truncation shall be given
51 to the calling process.
52
53 If the value of msgsz is greater than {SSIZE_MAX}, the result is imple‐
54 mentation-defined.
55
56 The argument msgtyp specifies the type of message requested as follows:
57
58 * If msgtyp is 0, the first message on the queue shall be received.
59
60 * If msgtyp is greater than 0, the first message of type msgtyp shall
61 be received.
62
63 * If msgtyp is less than 0, the first message of the lowest type that
64 is less than or equal to the absolute value of msgtyp shall be
65 received.
66
67 The argument msgflg specifies the action to be taken if a message of
68 the desired type is not on the queue. These are as follows:
69
70 * If (msgflg & IPC_NOWAIT) is non-zero, the calling thread shall
71 return immediately with a return value of -1 and errno set to
72 [ENOMSG].
73
74 * If (msgflg & IPC_NOWAIT) is 0, the calling thread shall suspend
75 execution until one of the following occurs:
76
77 -- A message of the desired type is placed on the queue.
78
79 -- The message queue identifier msqid is removed from the system;
80 when this occurs, errno shall be set to [EIDRM] and -1 shall be
81 returned.
82
83 -- The calling thread receives a signal that is to be caught; in
84 this case a message is not received and the calling thread
85 resumes execution in the manner prescribed in sigaction().
86
87 Upon successful completion, the following actions are taken with
88 respect to the data structure associated with msqid:
89
90 * msg_qnum shall be decremented by 1.
91
92 * msg_lrpid shall be set to the process ID of the calling process.
93
94 * msg_rtime shall be set to the current time, as described in Section
95 2.7.1, IPC General Description.
96
98 Upon successful completion, msgrcv() shall return a value equal to the
99 number of bytes actually placed into the buffer mtext. Otherwise, no
100 message shall be received, msgrcv() shall return -1, and errno shall be
101 set to indicate the error.
102
104 The msgrcv() function shall fail if:
105
106 E2BIG The value of mtext is greater than msgsz and (msgflg & MSG_NOER‐
107 ROR) is 0.
108
109 EACCES Operation permission is denied to the calling process; see Sec‐
110 tion 2.7, XSI Interprocess Communication.
111
112 EIDRM The message queue identifier msqid is removed from the system.
113
114 EINTR The msgrcv() function was interrupted by a signal.
115
116 EINVAL msqid is not a valid message queue identifier.
117
118 ENOMSG The queue does not contain a message of the desired type and
119 (msgflg & IPC_NOWAIT) is non-zero.
120
121 The following sections are informative.
122
124 Receiving a Message
125 The following example receives the first message on the queue (based on
126 the value of the msgtyp argument, 0). The queue is identified by the
127 msqid argument (assuming that the value has previously been set). This
128 call specifies that an error should be reported if no message is avail‐
129 able, but not if the message is too large. The message size is calcu‐
130 lated directly using the sizeof operator.
131
132
133 #include <sys/msg.h>
134 ...
135 int result;
136 int msqid;
137 struct message {
138 long type;
139 char text[20];
140 } msg;
141 long msgtyp = 0;
142 ...
143 result = msgrcv(msqid, (void *) &msg, sizeof(msg.text),
144 msgtyp, MSG_NOERROR | IPC_NOWAIT);
145
147 The POSIX Realtime Extension defines alternative interfaces for inter‐
148 process communication (IPC). Application developers who need to use IPC
149 should design their applications so that modules using the IPC routines
150 described in Section 2.7, XSI Interprocess Communication can be easily
151 modified to use the alternative interfaces.
152
154 None.
155
157 None.
158
160 Section 2.7, XSI Interprocess Communication, Section 2.8, Realtime,
161 mq_close(), mq_getattr(), mq_notify(), mq_open(), mq_receive(),
162 mq_send(), mq_setattr(), mq_unlink(), msgctl(), msgget(), msgsnd(),
163 sigaction()
164
165 The Base Definitions volume of POSIX.1‐2017, Section 3.226, Message
166 Queue, <sys_msg.h>
167
169 Portions of this text are reprinted and reproduced in electronic form
170 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
171 table Operating System Interface (POSIX), The Open Group Base Specifi‐
172 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
173 Electrical and Electronics Engineers, Inc and The Open Group. In the
174 event of any discrepancy between this version and the original IEEE and
175 The Open Group Standard, the original IEEE and The Open Group Standard
176 is the referee document. The original Standard can be obtained online
177 at http://www.opengroup.org/unix/online.html .
178
179 Any typographical or formatting errors that appear in this page are
180 most likely to have been introduced during the conversion of the source
181 files to man page format. To report such errors, see https://www.ker‐
182 nel.org/doc/man-pages/reporting_bugs.html .
183
184
185
186IEEE/The Open Group 2017 MSGRCV(3P)