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