sl@0
|
1 |
/** @file ../include/sys/msg.h
|
sl@0
|
2 |
@internalComponent
|
sl@0
|
3 |
*/
|
sl@0
|
4 |
|
sl@0
|
5 |
/** @fn msgget(key_t key, int msgflg)
|
sl@0
|
6 |
@param key
|
sl@0
|
7 |
@param msgflg
|
sl@0
|
8 |
@return Upon successful completion a positive message queue identifier is returned.
|
sl@0
|
9 |
Otherwise, -1 is returned and the global variable errno is set to indicate the error.
|
sl@0
|
10 |
|
sl@0
|
11 |
The msgget function
|
sl@0
|
12 |
returns the message queue identifier associated with key. A message queue identifier is a unique integer greater than zero.
|
sl@0
|
13 |
|
sl@0
|
14 |
A message queue is created if either key is equal to IPC_PRIVATE, or key does not have a message queue identifier associated with it and
|
sl@0
|
15 |
the IPC_CREAT bit is set in msgflg.
|
sl@0
|
16 |
|
sl@0
|
17 |
If a new message queue is created, the data structure associated with it (the msqid_ds structure, see msgctl is initialized as follows: msg_perm.cuid and msg_perm.uid are set to the effective uid of the calling process.
|
sl@0
|
18 |
In the current implementation, uid is set to root. msg_perm.gid and msg_perm.cgid are set to the effective gid of the calling process.
|
sl@0
|
19 |
In the current implementation, gid is set to root. msg_perm.mode is set to the lower 9 bits of msgflg. msg_cbytes, msg_qnum, msg_lspid, msg_lrpid, msg_rtime, and msg_stime are set to 0. msg_qbytes is set to the system wide maximum value for the number of bytes in a queue (Dv MSGMNB). msg_ctime is set to the current time.
|
sl@0
|
20 |
|
sl@0
|
21 |
Examples:
|
sl@0
|
22 |
|
sl@0
|
23 |
Example code to create a message queue using msgget
|
sl@0
|
24 |
|
sl@0
|
25 |
@code
|
sl@0
|
26 |
#include <sys/types.h>
|
sl@0
|
27 |
#include <sys/ipc.h>
|
sl@0
|
28 |
#include <sys/msg.h>
|
sl@0
|
29 |
#include <stdio.h>
|
sl@0
|
30 |
#include <string.h>
|
sl@0
|
31 |
#include <errno.h>
|
sl@0
|
32 |
|
sl@0
|
33 |
#define MESSAGE_Q_KEY 1000
|
sl@0
|
34 |
|
sl@0
|
35 |
int main(void)
|
sl@0
|
36 |
{
|
sl@0
|
37 |
int msq_id, len;
|
sl@0
|
38 |
struct {
|
sl@0
|
39 |
long mtype;
|
sl@0
|
40 |
char mtext[128];
|
sl@0
|
41 |
} msg_buf;
|
sl@0
|
42 |
/*
|
sl@0
|
43 |
* Create a message queue with a given key
|
sl@0
|
44 |
*/
|
sl@0
|
45 |
if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
|
sl@0
|
46 |
printf("Message Q create failed with errno %d
|
sl@0
|
47 |
", errno);
|
sl@0
|
48 |
return -1;
|
sl@0
|
49 |
}
|
sl@0
|
50 |
msg_buf.mtype = 1; /* message identifier */
|
sl@0
|
51 |
strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
|
sl@0
|
52 |
len = strlen(msg_buf.mtext)+1;
|
sl@0
|
53 |
/*
|
sl@0
|
54 |
* Put the message in the queue
|
sl@0
|
55 |
*/
|
sl@0
|
56 |
if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
|
sl@0
|
57 |
printf("Message Q send failed with errno %d
|
sl@0
|
58 |
", errno);
|
sl@0
|
59 |
}
|
sl@0
|
60 |
return 0;
|
sl@0
|
61 |
}
|
sl@0
|
62 |
|
sl@0
|
63 |
@endcode
|
sl@0
|
64 |
Example code to return an existing message queue with msgget
|
sl@0
|
65 |
@code
|
sl@0
|
66 |
#include <sys/types.h>
|
sl@0
|
67 |
#include <sys/ipc.h>
|
sl@0
|
68 |
#include <sys/msg.h>
|
sl@0
|
69 |
#include <stdio.h>
|
sl@0
|
70 |
#include <string.h>
|
sl@0
|
71 |
#include <errno.h>
|
sl@0
|
72 |
|
sl@0
|
73 |
#define MESSAGE_Q_KEY 1000
|
sl@0
|
74 |
|
sl@0
|
75 |
int main(void)
|
sl@0
|
76 |
{
|
sl@0
|
77 |
int msq_id;
|
sl@0
|
78 |
int msg_len = 128;
|
sl@0
|
79 |
int msg_type = 0; /* Any type of message */
|
sl@0
|
80 |
struct {
|
sl@0
|
81 |
long mtype;
|
sl@0
|
82 |
char mtext[128];
|
sl@0
|
83 |
} msg_buf;
|
sl@0
|
84 |
/*
|
sl@0
|
85 |
* Get the message queue id for the given key
|
sl@0
|
86 |
*/
|
sl@0
|
87 |
if ((msq_id = msgget(MESSAGE_Q_KEY, 0)) == -1) {
|
sl@0
|
88 |
printf("Message Q get id failed with errno %d
|
sl@0
|
89 |
", errno);
|
sl@0
|
90 |
return -1;
|
sl@0
|
91 |
}
|
sl@0
|
92 |
/*
|
sl@0
|
93 |
* Get the message from the queue
|
sl@0
|
94 |
*/
|
sl@0
|
95 |
if (msgrcv(msq_id, (struct msgbuf *)&msg;_buf, msg_len, msg_type, 0) == -1) {
|
sl@0
|
96 |
printf("Message Q recv failed with errno %d
|
sl@0
|
97 |
", errno);
|
sl@0
|
98 |
}
|
sl@0
|
99 |
/*
|
sl@0
|
100 |
* Remove the message queue
|
sl@0
|
101 |
*/
|
sl@0
|
102 |
if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
|
sl@0
|
103 |
printf("Message Q delete failed with errno %d
|
sl@0
|
104 |
", errno);
|
sl@0
|
105 |
return -1;
|
sl@0
|
106 |
}
|
sl@0
|
107 |
return 0;
|
sl@0
|
108 |
}
|
sl@0
|
109 |
|
sl@0
|
110 |
@endcode
|
sl@0
|
111 |
@see msgctl()
|
sl@0
|
112 |
@see msgrcv()
|
sl@0
|
113 |
@see msgsnd()
|
sl@0
|
114 |
|
sl@0
|
115 |
|
sl@0
|
116 |
|
sl@0
|
117 |
|
sl@0
|
118 |
@publishedAll
|
sl@0
|
119 |
@externallyDefinedApi
|
sl@0
|
120 |
*/
|
sl@0
|
121 |
|
sl@0
|
122 |
/** @fn msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
|
sl@0
|
123 |
@param msqid
|
sl@0
|
124 |
@param msgp
|
sl@0
|
125 |
@param msgsz
|
sl@0
|
126 |
@param msgflg
|
sl@0
|
127 |
|
sl@0
|
128 |
@return The msgsnd function returns the value 0 if successful; otherwise the
|
sl@0
|
129 |
value -1 is returned and errno is set to indicate the error.
|
sl@0
|
130 |
|
sl@0
|
131 |
The msgsnd function sends a message to the message queue specified in msqid. The msgp argument points to a structure containing the message. This structure should consist of the following members:
|
sl@0
|
132 |
@code
|
sl@0
|
133 |
long mtype; /* message type */
|
sl@0
|
134 |
char mtext[1]; /* body of message */
|
sl@0
|
135 |
@endcode
|
sl@0
|
136 |
|
sl@0
|
137 |
mtype is an integer greater than 0 that can be used for selecting messages (see msgrcv mtext is an array of bytes, with a size up to that of the system limit (Dv MSGMAX).
|
sl@0
|
138 |
|
sl@0
|
139 |
If the number of bytes already on the message queue plus msgsz is bigger than the maximum number of bytes on the message queue (Va msg_qbytes, see msgctl or the number of messages on all queues system-wide is already equal to the system limit, msgflg determines the action of msgsnd. If msgflg has IPC_NOWAIT mask set in it, the call will return immediately. If msgflg does not have IPC_NOWAIT set in it, the call will block until:
|
sl@0
|
140 |
|
sl@0
|
141 |
The condition which caused the call to block does no longer exist. The message will be sent.
|
sl@0
|
142 |
The message queue is removed, in which case -1 will be returned, and errno is set to EINVAL.
|
sl@0
|
143 |
After a successful call, the data structure associated with the message queue is updated in the following way:
|
sl@0
|
144 |
|
sl@0
|
145 |
msg_cbytes is incremented by the size of the message.
|
sl@0
|
146 |
msg_qnum is incremented by 1.
|
sl@0
|
147 |
msg_lspid is set to the pid of the calling process.
|
sl@0
|
148 |
msg_stime is set to the current time.
|
sl@0
|
149 |
|
sl@0
|
150 |
|
sl@0
|
151 |
Examples:
|
sl@0
|
152 |
@code
|
sl@0
|
153 |
#include <sys/types.h>
|
sl@0
|
154 |
#include <sys/ipc.h>
|
sl@0
|
155 |
#include <sys/msg.h>
|
sl@0
|
156 |
#include <stdio.h>
|
sl@0
|
157 |
#include <string.h>
|
sl@0
|
158 |
#include <errno.h>
|
sl@0
|
159 |
|
sl@0
|
160 |
#define MESSAGE_Q_KEY 1000
|
sl@0
|
161 |
|
sl@0
|
162 |
int main(void)
|
sl@0
|
163 |
{
|
sl@0
|
164 |
int msq_id, len;
|
sl@0
|
165 |
struct {
|
sl@0
|
166 |
long mtype;
|
sl@0
|
167 |
char mtext[128];
|
sl@0
|
168 |
} msg_buf;
|
sl@0
|
169 |
/*
|
sl@0
|
170 |
* Create a message queue with the given key
|
sl@0
|
171 |
*/
|
sl@0
|
172 |
if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
|
sl@0
|
173 |
printf("Message Q create failed with errno %d
|
sl@0
|
174 |
", errno);
|
sl@0
|
175 |
return -1;
|
sl@0
|
176 |
}
|
sl@0
|
177 |
msg_buf.mtype = 1; /* message identifier */
|
sl@0
|
178 |
strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
|
sl@0
|
179 |
len = strlen(msg_buf.mtext)+1;
|
sl@0
|
180 |
/*
|
sl@0
|
181 |
* Put the message in the queue
|
sl@0
|
182 |
*/
|
sl@0
|
183 |
if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
|
sl@0
|
184 |
printf("Message Q send failed with errno %d
|
sl@0
|
185 |
", errno);
|
sl@0
|
186 |
}
|
sl@0
|
187 |
return 0;
|
sl@0
|
188 |
}
|
sl@0
|
189 |
|
sl@0
|
190 |
@endcode
|
sl@0
|
191 |
|
sl@0
|
192 |
|
sl@0
|
193 |
@publishedAll
|
sl@0
|
194 |
@externallyDefinedApi
|
sl@0
|
195 |
*/
|
sl@0
|
196 |
|
sl@0
|
197 |
/** @fn msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
|
sl@0
|
198 |
@param msqid
|
sl@0
|
199 |
@param msgp
|
sl@0
|
200 |
@param msgsz
|
sl@0
|
201 |
@param msgtyp
|
sl@0
|
202 |
@param msgflg
|
sl@0
|
203 |
@return Upon successful completion, msgrcv returns the number of bytes received into the mtext field of the structure pointed to by msgp. Otherwise, -1 is returned, and errno set to indicate the error.
|
sl@0
|
204 |
|
sl@0
|
205 |
The msgrcv function receives a message from the message queue specified
|
sl@0
|
206 |
in msqid and places it into the structure pointed to by msgp. This structure should consist of the following members:
|
sl@0
|
207 |
@code
|
sl@0
|
208 |
long mtype; /* message type */
|
sl@0
|
209 |
char mtext[1]; /* body of message */
|
sl@0
|
210 |
@endcode
|
sl@0
|
211 |
|
sl@0
|
212 |
mtype is an integer greater than 0 that can be used for selecting messages, mtext is an array of bytes with a size up to that of the system limit (Dv MSGMAX).
|
sl@0
|
213 |
|
sl@0
|
214 |
The value of msgtyp has one of the following meanings: The msgtyp argument
|
sl@0
|
215 |
is greater than 0.
|
sl@0
|
216 |
The first message of type msgtyp will be received. The msgtyp argument
|
sl@0
|
217 |
is equal to 0.
|
sl@0
|
218 |
The first message on the queue (of any message type) will be received. The msgtyp argument
|
sl@0
|
219 |
is less than 0.
|
sl@0
|
220 |
The first message of the lowest message type that is
|
sl@0
|
221 |
less than or equal to the absolute value of msgtyp will be received.
|
sl@0
|
222 |
|
sl@0
|
223 |
The msgsz argument
|
sl@0
|
224 |
specifies the maximum length of the requested message.
|
sl@0
|
225 |
If the received
|
sl@0
|
226 |
message has a length greater than msgsz it will be silently truncated if the MSG_NOERROR flag is set in msgflg, otherwise an error will be returned.
|
sl@0
|
227 |
|
sl@0
|
228 |
If no matching message is present on the message queue specified by msqid, the behavior of msgrcv depends on whether the IPC_NOWAIT flag is set in msgflg or not. If IPC_NOWAIT is set, msgrcv will immediately return a value of -1 and set errno to ENOMSG. If IPC_NOWAIT is not set, the calling process will be blocked until: A message of the requested type becomes available on the message queue. The message queue is removed, in which case -1 will be returned and errno set to EINVAL.
|
sl@0
|
229 |
|
sl@0
|
230 |
If a message is successfully received, the data structure associated with msqid is updated as follows: msg_cbytes is decremented by the size of the message. msg_lrpid is set to the pid of the caller. msg_lrtime is set to the current time. msg_qnum is decremented by 1.
|
sl@0
|
231 |
|
sl@0
|
232 |
Examples:
|
sl@0
|
233 |
|
sl@0
|
234 |
Receive a message from the queue
|
sl@0
|
235 |
|
sl@0
|
236 |
@code
|
sl@0
|
237 |
#include <sys/types.h>
|
sl@0
|
238 |
#include <sys/ipc.h>
|
sl@0
|
239 |
#include <sys/msg.h>
|
sl@0
|
240 |
#include <stdio.h>
|
sl@0
|
241 |
#include <string.h>
|
sl@0
|
242 |
#include <errno.h>
|
sl@0
|
243 |
|
sl@0
|
244 |
#define MESSAGE_Q_KEY 1000
|
sl@0
|
245 |
|
sl@0
|
246 |
int main(void)
|
sl@0
|
247 |
{
|
sl@0
|
248 |
int msq_id;
|
sl@0
|
249 |
int msg_len = 128;
|
sl@0
|
250 |
int msg_type = 0; /* Any type of message */
|
sl@0
|
251 |
struct {
|
sl@0
|
252 |
long mtype;
|
sl@0
|
253 |
char mtext[128];
|
sl@0
|
254 |
} msg_buf;
|
sl@0
|
255 |
int ret_val = -1;
|
sl@0
|
256 |
/*
|
sl@0
|
257 |
* Get the message queue id for the given key
|
sl@0
|
258 |
*/
|
sl@0
|
259 |
if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT)) == -1) {
|
sl@0
|
260 |
printf("Message Q get id failed with errno %d
|
sl@0
|
261 |
", errno);
|
sl@0
|
262 |
return -1;
|
sl@0
|
263 |
}
|
sl@0
|
264 |
/*
|
sl@0
|
265 |
* Get the message from the queue
|
sl@0
|
266 |
*/
|
sl@0
|
267 |
if ((ret_val = msgrcv(msq_id, (struct msgbuf *)&msg;_buf, msg_len, msg_type, 0)) == -1) {
|
sl@0
|
268 |
printf("Message Q recv failed with errno %d
|
sl@0
|
269 |
", errno);
|
sl@0
|
270 |
}
|
sl@0
|
271 |
/*
|
sl@0
|
272 |
* Remove the message queue
|
sl@0
|
273 |
*/
|
sl@0
|
274 |
if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
|
sl@0
|
275 |
printf("Message Q delete failed with errno %d
|
sl@0
|
276 |
", errno);
|
sl@0
|
277 |
return -1;
|
sl@0
|
278 |
}
|
sl@0
|
279 |
return ret_val;
|
sl@0
|
280 |
}
|
sl@0
|
281 |
|
sl@0
|
282 |
|
sl@0
|
283 |
Send a message to the queue
|
sl@0
|
284 |
|
sl@0
|
285 |
#include <sys/types.h>
|
sl@0
|
286 |
#include <sys/ipc.h>
|
sl@0
|
287 |
#include <sys/msg.h>
|
sl@0
|
288 |
#include <stdio.h>
|
sl@0
|
289 |
#include <string.h>
|
sl@0
|
290 |
#include <errno.h>
|
sl@0
|
291 |
|
sl@0
|
292 |
#define MESSAGE_Q_KEY 1000
|
sl@0
|
293 |
|
sl@0
|
294 |
int main(void)
|
sl@0
|
295 |
{
|
sl@0
|
296 |
int msq_id, len;
|
sl@0
|
297 |
struct {
|
sl@0
|
298 |
long mtype;
|
sl@0
|
299 |
char mtext[128];
|
sl@0
|
300 |
} msg_buf;
|
sl@0
|
301 |
/*
|
sl@0
|
302 |
* Create a message queue with the given key
|
sl@0
|
303 |
*/
|
sl@0
|
304 |
if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
|
sl@0
|
305 |
printf("Message Q create failed with errno %d0, errno);
|
sl@0
|
306 |
return -1;
|
sl@0
|
307 |
}
|
sl@0
|
308 |
msg_buf.mtype = 1; /* message identifier */
|
sl@0
|
309 |
strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
|
sl@0
|
310 |
len = strlen(msg_buf.mtext)+1;
|
sl@0
|
311 |
/*
|
sl@0
|
312 |
* Put the message in the queue
|
sl@0
|
313 |
*/
|
sl@0
|
314 |
if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
|
sl@0
|
315 |
printf("Message Q send failed with errno %d
|
sl@0
|
316 |
", errno);
|
sl@0
|
317 |
}
|
sl@0
|
318 |
return 0;
|
sl@0
|
319 |
}
|
sl@0
|
320 |
|
sl@0
|
321 |
@endcode
|
sl@0
|
322 |
@see msgctl()
|
sl@0
|
323 |
@see msgget()
|
sl@0
|
324 |
@see msgsnd()
|
sl@0
|
325 |
|
sl@0
|
326 |
|
sl@0
|
327 |
|
sl@0
|
328 |
|
sl@0
|
329 |
@publishedAll
|
sl@0
|
330 |
@externallyDefinedApi
|
sl@0
|
331 |
*/
|
sl@0
|
332 |
|
sl@0
|
333 |
/** @fn msgctl(int msqid, int cmd, struct msqid_ds *buf)
|
sl@0
|
334 |
@param msqid
|
sl@0
|
335 |
@param cmd
|
sl@0
|
336 |
@param buf
|
sl@0
|
337 |
@return The msgctl function returns the value 0 if successful; otherwise the
|
sl@0
|
338 |
value -1 is returned and errno is set to indicate the error.
|
sl@0
|
339 |
|
sl@0
|
340 |
The msgctl system call performs some control operations on the message queue specified by msqid.
|
sl@0
|
341 |
Each message queue has a data structure associated with it, parts of which may be altered by msgctl and parts of which determine the actions of msgctl. The data structure is defined in \<sys/msg.h\> and contains (amongst others) the following members:
|
sl@0
|
342 |
|
sl@0
|
343 |
@code
|
sl@0
|
344 |
struct msqid_ds {
|
sl@0
|
345 |
struct ipc_perm msg_perm; /* msg queue permission bits */
|
sl@0
|
346 |
struct msg *msg_first; /* first message in the queue */
|
sl@0
|
347 |
struct msg *msg_last; /* last message in the queue */
|
sl@0
|
348 |
u_long msg_cbytes; /* number of bytes in use on the queue */
|
sl@0
|
349 |
u_long msg_qnum; /* number of msgs in the queue */
|
sl@0
|
350 |
u_long msg_qbytes; /* max # of bytes on the queue */
|
sl@0
|
351 |
pid_t msg_lspid; /* pid of last msgsnd() */
|
sl@0
|
352 |
pid_t msg_lrpid; /* pid of last msgrcv() */
|
sl@0
|
353 |
time_t msg_stime; /* time of last msgsnd() */
|
sl@0
|
354 |
long msg_pad1;
|
sl@0
|
355 |
time_t msg_rtime; /* time of last msgrcv() */
|
sl@0
|
356 |
long msg_pad2;
|
sl@0
|
357 |
time_t msg_ctime; /* time of last msgctl() */
|
sl@0
|
358 |
long msg_pad3;
|
sl@0
|
359 |
long msg_pad4[4];
|
sl@0
|
360 |
};
|
sl@0
|
361 |
@endcode
|
sl@0
|
362 |
|
sl@0
|
363 |
The ipc_perm structure used inside the shmid_ds structure is defined in \<sys/ipc.h\> and looks like this:
|
sl@0
|
364 |
@code
|
sl@0
|
365 |
struct ipc_perm {
|
sl@0
|
366 |
ushort cuid; /* creator user id */
|
sl@0
|
367 |
ushort cgid; /* creator group id */
|
sl@0
|
368 |
ushort uid; /* user id */
|
sl@0
|
369 |
ushort gid; /* group id */
|
sl@0
|
370 |
ushort mode; /* r/w permission */
|
sl@0
|
371 |
ushort seq; /* sequence # (to generate unique msg/sem/shm id) */
|
sl@0
|
372 |
key_t key; /* user specified msg/sem/shm key */
|
sl@0
|
373 |
};
|
sl@0
|
374 |
@endcode
|
sl@0
|
375 |
|
sl@0
|
376 |
|
sl@0
|
377 |
|
sl@0
|
378 |
The operation to be performed by msgctl is specified in cmd and is one of: IPC_STAT Gather information about the message queue and place it in the structure pointed to by buf.
|
sl@0
|
379 |
IPC_SET Set the value of the msg_perm.uid, msg_perm.gid, msg_perm.mode and msg_qbytes fields in the structure associated with msqid. The values are taken from the corresponding fields in the structure pointed to by buf. Values for msg_qbytes that exceed the system limit (MSGMNB from \<sys/msg.h\>) are silently truncated to that limit.
|
sl@0
|
380 |
IPC_RMID Remove the message queue specified by msqid and destroy the data associated with it.
|
sl@0
|
381 |
|
sl@0
|
382 |
|
sl@0
|
383 |
|
sl@0
|
384 |
The permission to read from or write to a message queue (see msgsnd and msgrcv is determined by the msg_perm.mode field in the same way as is done with files (see chmod)
|
sl@0
|
385 |
|
sl@0
|
386 |
|
sl@0
|
387 |
|
sl@0
|
388 |
Examples:
|
sl@0
|
389 |
@code
|
sl@0
|
390 |
#include <sys/types.h>
|
sl@0
|
391 |
#include <sys/ipc.h>
|
sl@0
|
392 |
#include <sys/msg.h>
|
sl@0
|
393 |
#include <stdio.h>
|
sl@0
|
394 |
#include <string.h>
|
sl@0
|
395 |
#include <errno.h>
|
sl@0
|
396 |
|
sl@0
|
397 |
#define MESSAGE_Q_KEY 1000
|
sl@0
|
398 |
|
sl@0
|
399 |
int main(void)
|
sl@0
|
400 |
{
|
sl@0
|
401 |
int msq_id, len;
|
sl@0
|
402 |
struct {
|
sl@0
|
403 |
long mtype;
|
sl@0
|
404 |
char mtext[128];
|
sl@0
|
405 |
} msg_buf;
|
sl@0
|
406 |
/*
|
sl@0
|
407 |
* Create a message queue with a given key
|
sl@0
|
408 |
*/
|
sl@0
|
409 |
if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
|
sl@0
|
410 |
printf("Message Q create failed with errno %d
|
sl@0
|
411 |
", errno);
|
sl@0
|
412 |
return -1;
|
sl@0
|
413 |
}
|
sl@0
|
414 |
msg_buf.mtype = 1; /* message identifier */
|
sl@0
|
415 |
strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
|
sl@0
|
416 |
len = strlen(msg_buf.mtext)+1;
|
sl@0
|
417 |
/*
|
sl@0
|
418 |
* Put the message in the queue
|
sl@0
|
419 |
*/
|
sl@0
|
420 |
if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
|
sl@0
|
421 |
printf("Message Q send failed with errno %d
|
sl@0
|
422 |
", errno);
|
sl@0
|
423 |
}
|
sl@0
|
424 |
return 0;
|
sl@0
|
425 |
}
|
sl@0
|
426 |
|
sl@0
|
427 |
@endcode
|
sl@0
|
428 |
@code
|
sl@0
|
429 |
#include <sys/types.h>
|
sl@0
|
430 |
#include <sys/ipc.h>
|
sl@0
|
431 |
#include <sys/msg.h>
|
sl@0
|
432 |
#include <stdio.h>
|
sl@0
|
433 |
#include <string.h>
|
sl@0
|
434 |
#include <errno.h>
|
sl@0
|
435 |
|
sl@0
|
436 |
#define MESSAGE_Q_KEY 1000
|
sl@0
|
437 |
|
sl@0
|
438 |
int main(void)
|
sl@0
|
439 |
{
|
sl@0
|
440 |
int msq_id;
|
sl@0
|
441 |
int msg_len = 128;
|
sl@0
|
442 |
int msg_type = 0; /* Any type of message */
|
sl@0
|
443 |
struct {
|
sl@0
|
444 |
long mtype;
|
sl@0
|
445 |
char mtext[128];
|
sl@0
|
446 |
} msg_buf;
|
sl@0
|
447 |
/*
|
sl@0
|
448 |
* Get the message queue id for the given key
|
sl@0
|
449 |
*/
|
sl@0
|
450 |
if ((msq_id = msgget(MESSAGE_Q_KEY, 0)) == -1) {
|
sl@0
|
451 |
printf("Message Q get id failed with errno %d
|
sl@0
|
452 |
", errno);
|
sl@0
|
453 |
return -1;
|
sl@0
|
454 |
}
|
sl@0
|
455 |
/*
|
sl@0
|
456 |
* Get the message from the queue
|
sl@0
|
457 |
*/
|
sl@0
|
458 |
if (msgrcv(msq_id, (struct msgbuf *)&msg;_buf, msg_len, msg_type, 0) == -1) {
|
sl@0
|
459 |
printf("Message Q recv failed with errno %d
|
sl@0
|
460 |
", errno);
|
sl@0
|
461 |
}
|
sl@0
|
462 |
/*
|
sl@0
|
463 |
* Remove the message queue
|
sl@0
|
464 |
*/
|
sl@0
|
465 |
if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
|
sl@0
|
466 |
printf("Message Q delete failed with errno %d
|
sl@0
|
467 |
", errno);
|
sl@0
|
468 |
return -1;
|
sl@0
|
469 |
}
|
sl@0
|
470 |
return 0;
|
sl@0
|
471 |
}
|
sl@0
|
472 |
|
sl@0
|
473 |
@endcode
|
sl@0
|
474 |
|
sl@0
|
475 |
Note :The below data members of the structure msqid_ds are internal components.
|
sl@0
|
476 |
@code
|
sl@0
|
477 |
struct msg *msg_first
|
sl@0
|
478 |
struct msg *msg_last;
|
sl@0
|
479 |
long msg_pad1;
|
sl@0
|
480 |
long msg_pad2;
|
sl@0
|
481 |
long msg_pad3;
|
sl@0
|
482 |
long msg_pad4[4];
|
sl@0
|
483 |
@endcode
|
sl@0
|
484 |
and are not filled if msgctl is called with the flag IPC_STAT.
|
sl@0
|
485 |
|
sl@0
|
486 |
@see msgget()
|
sl@0
|
487 |
@see msgrcv()
|
sl@0
|
488 |
@see msgsnd()
|
sl@0
|
489 |
|
sl@0
|
490 |
|
sl@0
|
491 |
|
sl@0
|
492 |
|
sl@0
|
493 |
@publishedAll
|
sl@0
|
494 |
@externallyDefinedApi
|
sl@0
|
495 |
*/
|
sl@0
|
496 |
|
sl@0
|
497 |
|
sl@0
|
498 |
/** @struct msqid_ds
|
sl@0
|
499 |
|
sl@0
|
500 |
The msqid_ds structure defines a message queue associated with a message queue ID. There is one queue per message queue ID.
|
sl@0
|
501 |
Collectively, the queues are stored as an array, with message queue IDs serving as an index into the array.
|
sl@0
|
502 |
Contains the following members,
|
sl@0
|
503 |
|
sl@0
|
504 |
@publishedAll
|
sl@0
|
505 |
@externallyDefinedApi
|
sl@0
|
506 |
*/
|
sl@0
|
507 |
|
sl@0
|
508 |
/** @var msqid_ds::msg_perm
|
sl@0
|
509 |
msg queue permission bits
|
sl@0
|
510 |
*/
|
sl@0
|
511 |
|
sl@0
|
512 |
/** @var msqid_ds::msg_first
|
sl@0
|
513 |
first message in the queue
|
sl@0
|
514 |
@internalComponent
|
sl@0
|
515 |
*/
|
sl@0
|
516 |
|
sl@0
|
517 |
/** @var msqid_ds::msg_last
|
sl@0
|
518 |
last message in the queue
|
sl@0
|
519 |
@internalComponent
|
sl@0
|
520 |
*/
|
sl@0
|
521 |
|
sl@0
|
522 |
/** @var msqid_ds::msg_cbytes
|
sl@0
|
523 |
number of bytes in use on the queue
|
sl@0
|
524 |
*/
|
sl@0
|
525 |
|
sl@0
|
526 |
/** @var msqid_ds::msg_qnum
|
sl@0
|
527 |
number of msgs in the queue
|
sl@0
|
528 |
*/
|
sl@0
|
529 |
|
sl@0
|
530 |
/** @var msqid_ds::msg_qbytes
|
sl@0
|
531 |
max n of bytes on the queue
|
sl@0
|
532 |
*/
|
sl@0
|
533 |
|
sl@0
|
534 |
/** @var msqid_ds::msg_lspid
|
sl@0
|
535 |
pid of last msgsnd()
|
sl@0
|
536 |
*/
|
sl@0
|
537 |
|
sl@0
|
538 |
/** @var msqid_ds::msg_lrpid
|
sl@0
|
539 |
pid of last msgrcv()
|
sl@0
|
540 |
*/
|
sl@0
|
541 |
|
sl@0
|
542 |
/** @var msqid_ds::msg_stime
|
sl@0
|
543 |
time of last msgsnd()
|
sl@0
|
544 |
*/
|
sl@0
|
545 |
|
sl@0
|
546 |
/** @var msqid_ds::msg_pad1
|
sl@0
|
547 |
time of last msgsnd()
|
sl@0
|
548 |
@internalComponent
|
sl@0
|
549 |
*/
|
sl@0
|
550 |
|
sl@0
|
551 |
/** @var msqid_ds::msg_rtime
|
sl@0
|
552 |
time of last msgrcv()
|
sl@0
|
553 |
*/
|
sl@0
|
554 |
|
sl@0
|
555 |
/** @var msqid_ds::msg_pad2
|
sl@0
|
556 |
time of last msgrcv()
|
sl@0
|
557 |
@internalComponent
|
sl@0
|
558 |
*/
|
sl@0
|
559 |
|
sl@0
|
560 |
/** @var msqid_ds::msg_ctime
|
sl@0
|
561 |
time of last msgctl()
|
sl@0
|
562 |
*/
|
sl@0
|
563 |
|
sl@0
|
564 |
/** @var msqid_ds::msg_pad3
|
sl@0
|
565 |
time of last msgctl()
|
sl@0
|
566 |
@internalComponent
|
sl@0
|
567 |
*/
|
sl@0
|
568 |
|
sl@0
|
569 |
/** @var msqid_ds::msg_pad4
|
sl@0
|
570 |
time of last msgctl()
|
sl@0
|
571 |
@internalComponent
|
sl@0
|
572 |
*/
|
sl@0
|
573 |
|
sl@0
|
574 |
|
sl@0
|
575 |
|
sl@0
|
576 |
/** @def MSG_NOERROR
|
sl@0
|
577 |
|
sl@0
|
578 |
The MSG_NOERROR identifier value, the msqid_ds struct and the msg struct are as defined by the SV API Intel 386 Processor Supplement.
|
sl@0
|
579 |
don't complain about too long msgs.
|
sl@0
|
580 |
|
sl@0
|
581 |
@publishedAll
|
sl@0
|
582 |
@externallyDefinedApi
|
sl@0
|
583 |
*/
|
sl@0
|
584 |
|
sl@0
|
585 |
|
sl@0
|
586 |
|
sl@0
|
587 |
|
sl@0
|
588 |
|