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

NAME

6       msgrcv - XSI message receive operation
7

SYNOPSIS

9       #include <sys/msg.h>
10
11       ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
12              int msgflg);
13
14

DESCRIPTION

16       The msgrcv() function operates on XSI message queues (see the Base Def‐
17       initions volume of IEEE Std 1003.1-2001, Section 3.224, Message Queue).
18       It is unspecified whether this function interoperates with the realtime
19       interprocess communication facilities defined in Realtime .
20
21       The msgrcv() function shall read a message from  the  queue  associated
22       with  the  message  queue identifier specified by msqid and place it in
23       the user-defined buffer pointed to by msgp.
24
25       The application shall ensure that the argument msgp points to  a  user-
26       defined  buffer that contains first a field of type long specifying the
27       type of the message, and then a data portion that holds the data  bytes
28       of  the  message.  The structure below is an example of what this user-
29       defined buffer might look like:
30
31
32              struct mymsg {
33                  long    mtype;     /* Message type. */
34                  char    mtext[1];  /* Message text. */
35              }
36
37       The structure member mtype is the received message's type as  specified
38       by the sending process.
39
40       The structure member mtext is the text of the message.
41
42       The  argument msgsz specifies the size in bytes of mtext.  The received
43       message shall be truncated to msgsz bytes if it is  larger  than  msgsz
44       and  (msgflg & MSG_NOERROR) is non-zero. The truncated part of the mes‐
45       sage shall be lost and no indication of the truncation shall  be  given
46       to the calling process.
47
48       If the value of msgsz is greater than {SSIZE_MAX}, the result is imple‐
49       mentation-defined.
50
51       The argument msgtyp specifies the type of message requested as follows:
52
53        * If msgtyp is 0, the first message on the queue shall be received.
54
55        * If msgtyp is greater than 0, the first message of type msgtyp  shall
56          be received.
57
58        * If  msgtyp is less than 0, the first message of the lowest type that
59          is less than or equal to the  absolute  value  of  msgtyp  shall  be
60          received.
61
62       The  argument  msgflg  specifies the action to be taken if a message of
63       the desired type is not on the queue. These are as follows:
64
65        * If (msgflg & IPC_NOWAIT)  is  non-zero,  the  calling  thread  shall
66          return  immediately  with  a  return  value  of  -1 and errno set to
67          [ENOMSG].
68
69        * If (msgflg & IPC_NOWAIT) is 0, the calling thread shall suspend exe‐
70          cution until one of the following occurs:
71
72           * A message of the desired type is placed on the queue.
73
74           * The  message  queue  identifier msqid is removed from the system;
75             when this occurs, errno shall be set  equal  to  [EIDRM]  and  -1
76             shall be returned.
77
78           * The  calling  thread  receives  a signal that is to be caught; in
79             this case a message  is  not  received  and  the  calling  thread
80             resumes execution in the manner prescribed in sigaction() .
81
82       Upon  successful  completion,  the  following  actions  are  taken with
83       respect to the data structure associated with msqid:
84
85        * msg_qnum shall be decremented by 1.
86
87        * msg_lrpid shall be set equal  to  the  process  ID  of  the  calling
88          process.
89
90        * msg_rtime shall be set equal to the current time.
91

RETURN VALUE

93       Upon  successful completion, msgrcv() shall return a value equal to the
94       number of bytes actually placed into the buffer  mtext.  Otherwise,  no
95       message shall be received, msgrcv() shall return (ssize_t)-1, and errno
96       shall be set to indicate the error.
97

ERRORS

99       The msgrcv() function shall fail if:
100
101       E2BIG  The value of mtext is greater than msgsz and (msgflg & MSG_NOER‐
102              ROR) is 0.
103
104       EACCES Operation  permission  is denied to the calling process; see XSI
105              Interprocess Communication .
106
107       EIDRM  The message queue identifier msqid is removed from the system.
108
109       EINTR  The msgrcv() function was interrupted by a signal.
110
111       EINVAL msqid is not a valid message queue identifier.
112
113       ENOMSG The queue does not contain a message of  the  desired  type  and
114              (msgflg & IPC_NOWAIT) is non-zero.
115
116
117       The following sections are informative.
118

EXAMPLES

120   Receiving a Message
121       The following example receives the first message on the queue (based on
122       the value of the msgtyp argument, 0). The queue is  identified  by  the
123       msqid  argument (assuming that the value has previously been set). This
124       call specifies that an error should be reported if no message is avail‐
125       able,  but  not if the message is too large. The message size is calcu‐
126       lated directly using the sizeof operator.
127
128
129              #include <sys/msg.h>
130              ...
131              int result;
132              int msqid;
133              struct message {
134                  long type;
135                  char text[20];
136              } msg;
137              long msgtyp = 0;
138              ...
139              result = msgrcv(msqid, (void *) &msg, sizeof(msg.text),
140                       msgtyp, MSG_NOERROR | IPC_NOWAIT);
141

APPLICATION USAGE

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

RATIONALE

150       None.
151

FUTURE DIRECTIONS

153       None.
154

SEE ALSO

156       XSI Interprocess Communication , Realtime , mq_close() , mq_getattr() ,
157       mq_notify()  ,  mq_open()  ,  mq_receive() , mq_send() , mq_setattr() ,
158       mq_unlink() , msgctl() , msgget() , msgsnd() , sigaction() ,  the  Base
159       Definitions volume of IEEE Std 1003.1-2001, <sys/msg.h>
160
162       Portions  of  this text are reprinted and reproduced in electronic form
163       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
164       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
165       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
166       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
167       event of any discrepancy between this version and the original IEEE and
168       The  Open Group Standard, the original IEEE and The Open Group Standard
169       is the referee document. The original Standard can be  obtained  online
170       at http://www.opengroup.org/unix/online.html .
171
172
173
174IEEE/The Open Group                  2003                            MSGRCV(P)
Impressum