|
|
Before a message can be sent or received, a uniquely identified message queue and data structure must be created. The unique identifier is called the message queue identifier (msqid); it is used to identify or refer to the associated message queue and data structure. This identifier is accessible by any process in the system, subject to normal access restrictions.
The message queue is used to store (header) information about each message being sent or received. This information, which is for internal use by the system, includes the following for each message:
There is one associated data structure for the uniquely identified message queue. This data structure contains the following information related to the message queue:
The definition for the associated message-queue data structure msqid_ds includes the following members:
struct msqid_ds { struct ipc_perm msg_perm; /* operation permission struct */ struct msg *msg_first; /* ptr to first message on q */ struct msg *msg_last; /* ptr to last message on q */ ulong msg_cbytes; /* current # bytes on q */ ulong msg_qnum; /* # of messages on q */ ulong msg_qbytes; /* max # of bytes on q */ pid_t msg_lspid; /* pid of last msgsnd */ pid_t msg_lrpid; /* pid of last msgrcv */ time_t msg_stime; /* last msgsnd time */ time_t msg_rtime; /* last msgrcv time */ time_t msg_ctime; /* last change time */ };
The C programming language data structure definition for the message-queue data structure msqid_ds is located in the sys/msg.h header file.
Note that the
msg_perm
member of
this structure uses
ipc_perm
as a template.
The figure below breaks out the operation permissions data structure.
In UnixWare, the definition of the
ipc_perm
data structure is as follows:
struct ipc_perm { uid_t uid; /* owner's user id */ gid_t gid; /* owner's group id */ uid_t cuid; /* creator's user id */ gid_t cgid; /* creator's group id */ mode_t mode; /* access modes */ ulong seq; /* slot usage sequence number */ key_t key; /* key */ long pad[4]; /* reserve area */ };
ipc_perm data structure
The C programming language data structure definition for the interprocess communication permissions data structure ipc_perm is located in the sys/ipc.h header file and is common to all IPC facilities.
The msgget system call is used to perform one of two tasks:
Both tasks require a key argument passed to the msgget system call. For the first task, if the key is not already in use for an existing message queue identifier, a new identifier is returned with an associated message queue and data structure created for the key.
There is also a provision for specifying a key of value zero, known as the private key (IPC_PRIVATE). When specified, a new identifier is always returned with an associated message queue and data structure created for it unless a system-tunable parameter would be exceeded. The ipcs command will show the key field for the msqid as all zeros.
For the second task, if a message queue identifier exists for the key specified, the value of the existing identifier is returned. If you do not want to have an existing message queue identifier returned, a control command (IPC_EXCL) can be specified (set) in the msgflg argument passed to the system call (see ``Using msgget'' for how to use this system call).
When performing the first task, the process that calls msgget becomes the owner/creator, and the associated data structure is initialized accordingly. Remember, ownership can be changed but the creating process always remains the creator. The message queue creator also determines the initial operation permissions for it.
Once a uniquely identified message queue and data structure are created, msgop (message operations) and msgctl (message control) can be used.
Message operations, as mentioned before, consist of sending and receiving messages. The msgsnd and msgrcv system calls are provided for each of these operations (see ``Operations for messages'' for details of the msgsnd and msgrcv system calls.
The msgctl system call permits you to control the message facility in the following ways: