1MSGOP(2) Linux Programmer's Manual MSGOP(2)
2
3
4
6 msgrcv, msgsnd - message operations
7
9 #include <sys/types.h>
10 #include <sys/ipc.h>
11 #include <sys/msg.h>
12
13 int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
14
15 ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,
16 int msgflg);
17
19 The msgsnd() and msgrcv() system calls are used, respectively, to send
20 messages to, and receive messages from, a message queue. The calling
21 process must have write permission on the message queue in order to
22 send a message, and read permission to receive a message.
23
24 The msgp argument is a pointer to caller-defined structure of the fol‐
25 lowing general form:
26
27 struct msgbuf {
28 long mtype; /* message type, must be > 0 */
29 char mtext[1]; /* message data */
30 };
31
32 The mtext field is an array (or other structure) whose size is speci‐
33 fied by msgsz, a non-negative integer value. Messages of zero length
34 (i.e., no mtext field) are permitted. The mtype field must have a
35 strictly positive integer value. This value can be used by the receiv‐
36 ing process for message selection (see the description of msgrcv()
37 below).
38
39 msgsnd()
40 The msgsnd() system call appends a copy of the message pointed to by
41 msgp to the message queue whose identifier is specified by msqid.
42
43 If sufficient space is available in the queue, msgsnd() succeeds imme‐
44 diately. (The queue capacity is defined by the msg_bytes field in the
45 associated data structure for the message queue. During queue creation
46 this field is initialized to MSGMNB bytes, but this limit can be modi‐
47 fied using msgctl(2).) If insufficient space is available in the
48 queue, then the default behavior of msgsnd() is to block until space
49 becomes available. If IPC_NOWAIT is specified in msgflg, then the call
50 instead fails with the error EAGAIN.
51
52 A blocked msgsnd() call may also fail if:
53
54 * the queue is removed, in which case the system call fails with errno
55 set to EIDRM; or
56
57 * a signal is caught, in which case the system call fails with errno
58 set to EINTR;see signal(7). (msgsnd() is never automatically
59 restarted after being interrupted by a signal handler, regardless of
60 the setting of the SA_RESTART flag when establishing a signal han‐
61 dler.)
62
63 Upon successful completion the message queue data structure is updated
64 as follows:
65
66 msg_lspid is set to the process ID of the calling process.
67
68 msg_qnum is incremented by 1.
69
70 msg_stime is set to the current time.
71
72 msgrcv()
73 The msgrcv() system call removes a message from the queue specified by
74 msqid and places it in the buffer pointed to by msgp.
75
76 The argument msgsz specifies the maximum size in bytes for the member
77 mtext of the structure pointed to by the msgp argument. If the message
78 text has length greater than msgsz, then the behavior depends on
79 whether MSG_NOERROR is specified in msgflg. If MSG_NOERROR is speci‐
80 fied, then the message text will be truncated (and the truncated part
81 will be lost); if MSG_NOERROR is not specified, then the message isn't
82 removed from the queue and the system call fails returning -1 with
83 errno set to E2BIG.
84
85 The argument msgtyp specifies the type of message requested as follows:
86
87 * If msgtyp is 0, then the first message in the queue is read.
88
89 * If msgtyp is greater than 0, then the first message in the queue of
90 type msgtyp is read, unless MSG_EXCEPT was specified in msgflg, in
91 which case the first message in the queue of type not equal to msgtyp
92 will be read.
93
94 * If msgtyp is less than 0, then the first message in the queue with
95 the lowest type less than or equal to the absolute value of msgtyp
96 will be read.
97
98 The msgflg argument is a bit mask constructed by ORing together zero or
99 more of the following flags:
100
101 IPC_NOWAIT
102 Return immediately if no message of the requested type is in the
103 queue. The system call fails with errno set to ENOMSG.
104
105 MSG_EXCEPT
106 Used with msgtyp greater than 0 to read the first message in the
107 queue with message type that differs from msgtyp.
108
109 MSG_NOERROR
110 To truncate the message text if longer than msgsz bytes.
111
112 If no message of the requested type is available and IPC_NOWAIT isn't
113 specified in msgflg, the calling process is blocked until one of the
114 following conditions occurs:
115
116 * A message of the desired type is placed in the queue.
117
118 * The message queue is removed from the system. In this case the sys‐
119 tem call fails with errno set to EIDRM.
120
121 * The calling process catches a signal. In this case the system call
122 fails with errno set to EINTR. (msgrcv() is never automatically
123 restarted after being interrupted by a signal handler, regardless of
124 the setting of the SA_RESTART flag when establishing a signal han‐
125 dler.)
126
127 Upon successful completion the message queue data structure is updated
128 as follows:
129
130 msg_lrpid is set to the process ID of the calling process.
131
132 msg_qnum is decremented by 1.
133
134 msg_rtime is set to the current time.
135
137 On failure both functions return -1 with errno indicating the error,
138 otherwise msgsnd() returns 0 and msgrcv() returns the number of bytes
139 actually copied into the mtext array.
140
142 When msgsnd() fails, errno will be set to one among the following val‐
143 ues:
144
145 EACCES The calling process does not have write permission on the mes‐
146 sage queue, and does not have the CAP_IPC_OWNER capability.
147
148 EAGAIN The message can't be sent due to the msg_qbytes limit for the
149 queue and IPC_NOWAIT was specified in msgflg.
150
151 EFAULT The address pointed to by msgp isn't accessible.
152
153 EIDRM The message queue was removed.
154
155 EINTR Sleeping on a full message queue condition, the process caught a
156 signal.
157
158 EINVAL Invalid msqid value, or non-positive mtype value, or invalid
159 msgsz value (less than 0 or greater than the system value MSG‐
160 MAX).
161
162 ENOMEM The system does not have enough memory to make a copy of the
163 message pointed to by msgp.
164
165 When msgrcv() fails, errno will be set to one among the following val‐
166 ues:
167
168 E2BIG The message text length is greater than msgsz and MSG_NOERROR
169 isn't specified in msgflg.
170
171 EACCES The calling process does not have read permission on the message
172 queue, and does not have the CAP_IPC_OWNER capability.
173
174 EAGAIN No message was available in the queue and IPC_NOWAIT was speci‐
175 fied in msgflg.
176
177 EFAULT The address pointed to by msgp isn't accessible.
178
179 EIDRM While the process was sleeping to receive a message, the message
180 queue was removed.
181
182 EINTR While the process was sleeping to receive a message, the process
183 caught a signal; see signal(7).
184
185 EINVAL msgqid was invalid, or msgsz was less than 0.
186
187 ENOMSG IPC_NOWAIT was specified in msgflg and no message of the
188 requested type existed on the message queue.
189
191 SVr4, POSIX.1-2001.
192
194 The msgp argument is declared as struct msgbuf * with libc4, libc5,
195 glibc 2.0, glibc 2.1. It is declared as void * with glibc 2.2 and
196 later, as required by SUSv2 and SUSv3.
197
198 The following limits on message queue resources affect the msgsnd()
199 call:
200
201 MSGMAX Maximum size for a message text: 8192 bytes (on Linux, this
202 limit can be read and modified via /proc/sys/kernel/msgmax).
203
204 MSGMNB Default maximum size in bytes of a message queue: 16384 bytes
205 (on Linux, this limit can be read and modified via
206 /proc/sys/kernel/msgmnb). The superuser can increase the size
207 of a message queue beyond MSGMNB by a msgctl(2) system call.
208
209 The implementation has no intrinsic limits for the system wide maximum
210 number of message headers (MSGTQL) and for the system wide maximum size
211 in bytes of the message pool (MSGPOOL).
212
214 msgctl(2), msgget(2), capabilities(7), mq_overview(7), svipc(7)
215
217 This page is part of release 3.22 of the Linux man-pages project. A
218 description of the project, and information about reporting bugs, can
219 be found at http://www.kernel.org/doc/man-pages/.
220
221
222
223Linux 2008-04-23 MSGOP(2)