Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


A process usually referred to as thesendergenerates messages and writes them to a queue, while one or
more other processes (logically known asreceivers) retrieve them from the queue. The individual message
elements contain the message text and a (positive) number to implement several types within a message
queue. Receivers can retrieve messages on the basis of the number (e.g., they can specify that they will
accept only messages with the number 1 or messages up to the number 5). Once a message has been read,
the kernel deletes it from the queue. Even if several processes are listening in on a channel, each message
can be read by one process only.


Messages with the same number are processed in FIFO order (first in, first out). Messages placed on
the queue first are retrieved first. However, if messages are read selectively, the FIFO order no longer
applies.


Sender and receiver need not be running at the same time in order to communicate
via message queues. For example, a sender process can open a queue, write
messages on it, and terminate its work. A receiver process started after the sender
has terminated can still access the queueand (by reference to the message number)
retrieve the messages intended for it. The messages are held by the kernel in the
intervening period.

Message queues are also implemented using a network of data structures similar to those already dis-
cussed. The starting point is the appropriateipc_idsinstance of the current namespace.


Again, the internal ID numbers are formally associated withkern_ipc_perminstances, but as in the
semaphore case, a different data type (struct msg_queue) is obtained as a result of type conversion. The
structure is defined as follows:


<msg.h>
struct msg_queue {
struct kern_ipc_perm q_perm;
time_t q_stime; /* last msgsnd time */
time_t q_rtime; /* last msgrcv time */
time_t q_ctime; /* last change time */
unsigned long q_cbytes; /* current number of bytes on queue */
unsigned long q_qnum; /* number of messages in queue */
unsigned long q_qbytes; /* max number of bytes on queue */
pid_t q_lspid; /* pid of last msgsnd */
pid_t q_lrpid; /* last receive pid */

struct list_head q_messages;
struct list_head q_receivers;
struct list_head q_senders;
};

The structure includes status information as well as queue access permissions.


❑ q_stime,q_rtimeandq_ctimespecify the last send, receive, and change time (if queue proper-
ties are changed).
❑ q_cbytesspecifies the number of bytes currently used by the messages in the queue.
Free download pdf