1MSGOP(2) Linux Programmer's Manual MSGOP(2)
2
3
4
6 msgop - 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, int
16 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 The msgsnd() system call appends a copy of the message pointed to by
40 msgp to the message queue whose identifier is specified by msqid.
41
42 If sufficient space is available in the queue, msgsnd() succeeds imme‐
43 diately. (The queue capacity is defined by the msg_bytes field in the
44 associated data structure for the message queue. During queue creation
45 this field is initialised to MSGMNB bytes, but this limit can be modi‐
46 fied using msgctl().) If insufficient space is available in the queue,
47 then the default behaviour of msgsnd() is to block until space becomes
48 available. If IPC_NOWAIT is specified in msgflg, then the call instead
49 fails with the error EAGAIN.
50
51 A blocked msgsnd() call may also fail if the queue is removed (in which
52 case the system call fails with errno set to EIDRM), or a signal is
53 caught (in which case the system call fails with errno set to EINTR).
54 (msgsnd and msgrcv are never automatically restarted after being inter‐
55 rupted by a signal handler, regardless of the setting of the
56 SA_RESTART flag when establishing a signal handler.)
57
58 Upon successful completion the message queue data structure is updated
59 as follows:
60
61 msg_lspid is set to the process ID of the calling process.
62
63 msg_qnum is incremented by 1.
64
65 msg_stime is set to the current time.
66
67 The system call msgrcv() removes a message from the queue specified by
68 msqid and places it in the buffer pointed to msgp.
69
70 The argument msgsz specifies the maximum size in bytes for the member
71 mtext of the structure pointed to by the msgp argument. If the message
72 text has length greater than msgsz, then the behaviour depends on
73 whether MSG_NOERROR is specified in msgflg. If MSG_NOERROR is speci‐
74 fied, then the message text will be truncated (and the truncated part
75 will be lost); if MSG_NOERROR is not specified, then the message isn't
76 removed from the queue and the system call fails returning -1 with
77 errno set to E2BIG.
78
79 The argument msgtyp specifies the type of message requested as follows:
80
81 If msgtyp is 0, then the first message in the queue is read.
82
83 If msgtyp is greater than 0, then the first message in the queue
84 of type msgtyp is read, unless MSG_EXCEPT was specified in
85 msgflg, in which case the first message in the queue of type not
86 equal to msgtyp will be read.
87
88 If msgtyp is less than 0, then the first message in the queue
89 with the lowest type less than or equal to the absolute value of
90 msgtyp will be read.
91
92 The msgflg argument is a bit mask constructed by ORing together zero or
93 more of the following flags:
94
95 IPC_NOWAIT
96 Return immediately if no message of the requested type is in the
97 queue. The system call fails with errno set to ENOMSG.
98
99 MSG_EXCEPT
100 Used with msgtyp greater than 0 to read the first message in the
101 queue with message type that differs from msgtyp.
102
103 MSG_NOERROR
104 To truncate the message text if longer than msgsz bytes.
105
106 If no message of the requested type is available and IPC_NOWAIT isn't
107 specified in msgflg, the calling process is blocked until one of the
108 following conditions occurs:
109
110 A message of the desired type is placed in the queue.
111
112 The message queue is removed from the system. In this case the
113 system call fails with errno set to EIDRM.
114
115 The calling process catches a signal. In this case the system
116 call fails with errno set to EINTR.
117
118 Upon successful completion the message queue data structure is updated
119 as follows:
120
121 msg_lrpid is set to the process ID of the calling process.
122
123 msg_qnum is decremented by 1.
124
125 msg_rtime is set to the current time.
126
128 On failure both functions return -1 with errno indicating the error,
129 otherwise msgsnd() returns 0 and msgrcv() returns the number of bytes
130 actually copied into the mtext array.
131
133 When msgsnd() fails, errno will be set to one among the following val‐
134 ues:
135
136 EACCES The calling process does not have write permission on the
137 message queue, and does not have the CAP_IPC_OWNER capabil‐
138 ity.
139
140 EAGAIN The message can't be sent due to the msg_qbytes limit for
141 the queue and IPC_NOWAIT was specified in msgflg.
142
143 EFAULT The address pointed to by msgp isn't accessible.
144
145 EIDRM The message queue was removed.
146
147 EINTR Sleeping on a full message queue condition, the process
148 caught a signal.
149
150 EINVAL Invalid msqid value, or non-positive mtype value, or invalid
151 msgsz value (less than 0 or greater than the system value
152 MSGMAX).
153
154 ENOMEM The system does not have enough memory to make a copy of the
155 message pointed to by msgp.
156
157 When msgrcv() fails, errno will be set to one among the following val‐
158 ues:
159
160 E2BIG The message text length is greater than msgsz and MSG_NOER‐
161 ROR isn't specified in msgflg.
162
163 EACCES The calling process does not have read permission on the
164 message queue, and does not have the CAP_IPC_OWNER capabil‐
165 ity.
166
167 EAGAIN No message was available in the queue and IPC_NOWAIT was
168 specified in msgflg.
169
170 EFAULT The address pointed to by msgp isn't accessible.
171
172 EIDRM While the process was sleeping to receive a message, the
173 message queue was removed.
174
175 EINTR While the process was sleeping to receive a message, the
176 process caught a signal.
177
178 EINVAL msgqid was invalid, or msgsz was less than 0.
179
180 ENOMSG IPC_NOWAIT was specified in msgflg and no message of the
181 requested type existed on the message queue.
182
184 SVr4, POSIX.1-2001.
185
187 The msgp argument is declared as struct msgbuf * with libc4, libc5,
188 glibc 2.0, glibc 2.1. It is declared as void * with glibc 2.2 and
189 later, as required by SUSv2 and SUSv3.
190
191 The following limits on message queue resources affect the msgsnd()
192 call:
193
194 MSGMAX Maximum size for a message text: 8192 bytes (on Linux, this
195 limit can be read and modified via /proc/sys/kernel/msgmax).
196
197 MSGMNB Default maximum size in bytes of a message queue: 16384
198 bytes (on Linux, this limit can be read and modified via
199 /proc/sys/kernel/msgmnb). The superuser can increase the
200 size of a message queue beyond MSGMNB by a msgctl() system
201 call.
202
203 The implementation has no intrinsic limits for the system wide maximum
204 number of message headers (MSGTQL) and for the system wide maximum size
205 in bytes of the message pool (MSGPOOL).
206
208 msgctl(2), msgget(2), msgrcv(2), msgsnd(2), capabilities(7), mq_over‐
209 view(7), svipc(7)
210
211
212
213Linux 2.6.15 2006-02-02 MSGOP(2)