1MSGRCV(3P)                 POSIX Programmer's Manual                MSGRCV(3P)
2
3
4

PROLOG

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
11

NAME

13       msgrcv — XSI message receive operation
14

SYNOPSIS

16       #include <sys/msg.h>
17
18       ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
19           int msgflg);
20

DESCRIPTION

22       The msgrcv() function operates on XSI message queues (see the Base Def‐
23       initions  volume of POSIX.1‐2008, Section 3.225, Message Queue).  It is
24       unspecified whether  this  function  interoperates  with  the  realtime
25       interprocess communication facilities defined in Section 2.8, 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           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

RETURN VALUE

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

ERRORS

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

EXAMPLES

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           #include <sys/msg.h>
133           ...
134           int result;
135           int msqid;
136           struct message {
137               long type;
138               char text[20];
139           } msg;
140           long msgtyp = 0;
141           ...
142           result = msgrcv(msqid, (void *) &msg, sizeof(msg.text),
143                    msgtyp, MSG_NOERROR | IPC_NOWAIT);
144

APPLICATION USAGE

146       The  POSIX Realtime Extension defines alternative interfaces for inter‐
147       process communication (IPC). Application developers who need to use IPC
148       should design their applications so that modules using the IPC routines
149       described in Section 2.7, XSI Interprocess Communication can be  easily
150       modified to use the alternative interfaces.
151

RATIONALE

153       None.
154

FUTURE DIRECTIONS

156       None.
157

SEE ALSO

159       Section  2.7,  XSI  Interprocess  Communication, Section 2.8, Realtime,
160       mq_close(),   mq_getattr(),   mq_notify(),   mq_open(),   mq_receive(),
161       mq_send(),  mq_setattr(),  mq_unlink(),  msgctl(),  msgget(), msgsnd(),
162       sigaction()
163
164       The Base Definitions volume of  POSIX.1‐2008,  Section  3.225,  Message
165       Queue, <sys_msg.h>
166
168       Portions  of  this text are reprinted and reproduced in electronic form
169       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
170       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
171       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
172       cal  and  Electronics  Engineers,  Inc  and  The  Open Group.  (This is
173       POSIX.1-2008 with the 2013 Technical Corrigendum  1  applied.)  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.unix.org/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                  2013                           MSGRCV(3P)
Impressum