First public contribution.
1 /** @file ../include/sys/msg.h
5 /** @fn msgget(key_t key, int msgflg)
8 @return Upon successful completion a positive message queue identifier is returned.
9 Otherwise, -1 is returned and the global variable errno is set to indicate the error.
12 returns the message queue identifier associated with key. A message queue identifier is a unique integer greater than zero.
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
15 the IPC_CREAT bit is set in msgflg.
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.
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.
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.
23 Example code to create a message queue using msgget
26 #include <sys/types.h>
33 #define MESSAGE_Q_KEY 1000
43 * Create a message queue with a given key
45 if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
46 printf("Message Q create failed with errno %d
50 msg_buf.mtype = 1; /* message identifier */
51 strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
52 len = strlen(msg_buf.mtext)+1;
54 * Put the message in the queue
56 if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
57 printf("Message Q send failed with errno %d
64 Example code to return an existing message queue with msgget
66 #include <sys/types.h>
73 #define MESSAGE_Q_KEY 1000
79 int msg_type = 0; /* Any type of message */
85 * Get the message queue id for the given key
87 if ((msq_id = msgget(MESSAGE_Q_KEY, 0)) == -1) {
88 printf("Message Q get id failed with errno %d
93 * Get the message from the queue
95 if (msgrcv(msq_id, (struct msgbuf *)&msg;_buf, msg_len, msg_type, 0) == -1) {
96 printf("Message Q recv failed with errno %d
100 * Remove the message queue
102 if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
103 printf("Message Q delete failed with errno %d
119 @externallyDefinedApi
122 /** @fn msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
128 @return The msgsnd function returns the value 0 if successful; otherwise the
129 value -1 is returned and errno is set to indicate the error.
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:
133 long mtype; /* message type */
134 char mtext[1]; /* body of message */
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).
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:
141 The condition which caused the call to block does no longer exist. The message will be sent.
142 The message queue is removed, in which case -1 will be returned, and errno is set to EINVAL.
143 After a successful call, the data structure associated with the message queue is updated in the following way:
145 msg_cbytes is incremented by the size of the message.
146 msg_qnum is incremented by 1.
147 msg_lspid is set to the pid of the calling process.
148 msg_stime is set to the current time.
153 #include <sys/types.h>
160 #define MESSAGE_Q_KEY 1000
170 * Create a message queue with the given key
172 if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
173 printf("Message Q create failed with errno %d
177 msg_buf.mtype = 1; /* message identifier */
178 strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
179 len = strlen(msg_buf.mtext)+1;
181 * Put the message in the queue
183 if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
184 printf("Message Q send failed with errno %d
194 @externallyDefinedApi
197 /** @fn msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg)
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.
205 The msgrcv function receives a message from the message queue specified
206 in msqid and places it into the structure pointed to by msgp. This structure should consist of the following members:
208 long mtype; /* message type */
209 char mtext[1]; /* body of message */
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).
214 The value of msgtyp has one of the following meanings: The msgtyp argument
216 The first message of type msgtyp will be received. The msgtyp argument
218 The first message on the queue (of any message type) will be received. The msgtyp argument
220 The first message of the lowest message type that is
221 less than or equal to the absolute value of msgtyp will be received.
224 specifies the maximum length of the requested message.
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.
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.
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.
234 Receive a message from the queue
237 #include <sys/types.h>
244 #define MESSAGE_Q_KEY 1000
250 int msg_type = 0; /* Any type of message */
257 * Get the message queue id for the given key
259 if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT)) == -1) {
260 printf("Message Q get id failed with errno %d
265 * Get the message from the queue
267 if ((ret_val = msgrcv(msq_id, (struct msgbuf *)&msg;_buf, msg_len, msg_type, 0)) == -1) {
268 printf("Message Q recv failed with errno %d
272 * Remove the message queue
274 if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
275 printf("Message Q delete failed with errno %d
283 Send a message to the queue
285 #include <sys/types.h>
292 #define MESSAGE_Q_KEY 1000
302 * Create a message queue with the given key
304 if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
305 printf("Message Q create failed with errno %d0, errno);
308 msg_buf.mtype = 1; /* message identifier */
309 strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
310 len = strlen(msg_buf.mtext)+1;
312 * Put the message in the queue
314 if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
315 printf("Message Q send failed with errno %d
330 @externallyDefinedApi
333 /** @fn msgctl(int msqid, int cmd, struct msqid_ds *buf)
337 @return The msgctl function returns the value 0 if successful; otherwise the
338 value -1 is returned and errno is set to indicate the error.
340 The msgctl system call performs some control operations on the message queue specified by msqid.
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:
345 struct ipc_perm msg_perm; /* msg queue permission bits */
346 struct msg *msg_first; /* first message in the queue */
347 struct msg *msg_last; /* last message in the queue */
348 u_long msg_cbytes; /* number of bytes in use on the queue */
349 u_long msg_qnum; /* number of msgs in the queue */
350 u_long msg_qbytes; /* max # of bytes on the queue */
351 pid_t msg_lspid; /* pid of last msgsnd() */
352 pid_t msg_lrpid; /* pid of last msgrcv() */
353 time_t msg_stime; /* time of last msgsnd() */
355 time_t msg_rtime; /* time of last msgrcv() */
357 time_t msg_ctime; /* time of last msgctl() */
363 The ipc_perm structure used inside the shmid_ds structure is defined in \<sys/ipc.h\> and looks like this:
366 ushort cuid; /* creator user id */
367 ushort cgid; /* creator group id */
368 ushort uid; /* user id */
369 ushort gid; /* group id */
370 ushort mode; /* r/w permission */
371 ushort seq; /* sequence # (to generate unique msg/sem/shm id) */
372 key_t key; /* user specified msg/sem/shm key */
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.
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.
380 IPC_RMID Remove the message queue specified by msqid and destroy the data associated with it.
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)
390 #include <sys/types.h>
397 #define MESSAGE_Q_KEY 1000
407 * Create a message queue with a given key
409 if ((msq_id = msgget(MESSAGE_Q_KEY, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
410 printf("Message Q create failed with errno %d
414 msg_buf.mtype = 1; /* message identifier */
415 strcpy(msg_buf.mtext, "some_data_to_send"); /* data */
416 len = strlen(msg_buf.mtext)+1;
418 * Put the message in the queue
420 if (msgsnd(msq_id, (struct msgbuf *)&msg;_buf, len, 0) == -1) {
421 printf("Message Q send failed with errno %d
429 #include <sys/types.h>
436 #define MESSAGE_Q_KEY 1000
442 int msg_type = 0; /* Any type of message */
448 * Get the message queue id for the given key
450 if ((msq_id = msgget(MESSAGE_Q_KEY, 0)) == -1) {
451 printf("Message Q get id failed with errno %d
456 * Get the message from the queue
458 if (msgrcv(msq_id, (struct msgbuf *)&msg;_buf, msg_len, msg_type, 0) == -1) {
459 printf("Message Q recv failed with errno %d
463 * Remove the message queue
465 if (msgctl(msq_id, IPC_RMID, NULL) == -1) {
466 printf("Message Q delete failed with errno %d
475 Note :The below data members of the structure msqid_ds are internal components.
477 struct msg *msg_first
478 struct msg *msg_last;
484 and are not filled if msgctl is called with the flag IPC_STAT.
494 @externallyDefinedApi
500 The msqid_ds structure defines a message queue associated with a message queue ID. There is one queue per message queue ID.
501 Collectively, the queues are stored as an array, with message queue IDs serving as an index into the array.
502 Contains the following members,
505 @externallyDefinedApi
508 /** @var msqid_ds::msg_perm
509 msg queue permission bits
512 /** @var msqid_ds::msg_first
513 first message in the queue
517 /** @var msqid_ds::msg_last
518 last message in the queue
522 /** @var msqid_ds::msg_cbytes
523 number of bytes in use on the queue
526 /** @var msqid_ds::msg_qnum
527 number of msgs in the queue
530 /** @var msqid_ds::msg_qbytes
531 max n of bytes on the queue
534 /** @var msqid_ds::msg_lspid
538 /** @var msqid_ds::msg_lrpid
542 /** @var msqid_ds::msg_stime
543 time of last msgsnd()
546 /** @var msqid_ds::msg_pad1
547 time of last msgsnd()
551 /** @var msqid_ds::msg_rtime
552 time of last msgrcv()
555 /** @var msqid_ds::msg_pad2
556 time of last msgrcv()
560 /** @var msqid_ds::msg_ctime
561 time of last msgctl()
564 /** @var msqid_ds::msg_pad3
565 time of last msgctl()
569 /** @var msqid_ds::msg_pad4
570 time of last msgctl()
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.
579 don't complain about too long msgs.
582 @externallyDefinedApi