ptg10805159
562 Interprocess Communication Chapter 15
The first function normally called ismsggetto either open an existing queue or
create a new queue.
#include <sys/msg.h>
int msgget(key_tkey,intflag);
Returns: message queue ID if OK,−1 on error
In Section 15.6.1, we described the rules for converting thekeyinto an identifier and
discussed whether a new queue is created or an existing queue is referenced. When a
new queue is created, the following members of themsqid_dsstructureare initialized.
•Theipc_permstructure is initialized as described in Section 15.6.2. Themode
member of this structure is set to the corresponding permission bits offlag.
These permissions arespecified with the values from Figure15.24.
- msg_qnum,msg_lspid,msg_lrpid,msg_stime,andmsg_rtimeareall set
to 0. - msg_ctimeis set to the current time.
- msg_qbytesis set to the system limit.
On success,msggetreturns the non-negative queue ID. This value is then used with
the other three message queue functions.
Themsgctlfunction performs various operations on a queue. This function and
the related functions for semaphores and shared memory (semctlandshmctl)are the
ioctl-like functions for XSI IPC (i.e., the garbage-can functions).
#include <sys/msg.h>
int msgctl(intmsqid,int cmd,struct msqid_ds *buf);
Returns: 0 if OK,−1 on error
Thecmdargument specifies the command to be performed on the queue specified by
msqid.
IPC_STAT Fetch themsqid_dsstructurefor this queue, storing it in the structure
pointed to bybuf.
IPC_SET Copy the following fields from the structurepointed to bybufto the
msqid_ds structureassociated with this queue: msg_perm.uid,
msg_perm.gid,msg_perm.mode,andmsg_qbytes.This command
can be executed only by a process whose effective user ID equals
msg_perm.cuidormsg_perm.uidor by a process with superuser
privileges. Only the superuser can increase the value ofmsg_qbytes.
IPC_RMID Remove the message queue from the system and any data still on the
queue. Thisremoval is immediate. Any other process still using the
message queue will get an error of EIDRM on its next attempted
operation on the queue. This command can be executed only by a
process whose effective user ID equals msg_perm.cuid or
msg_perm.uidor by a process with superuser privileges.