Message Queues

Another way of communicating between processes is to exchange messages. This is done using the message queue mechanism, whose implementation is based on the System V model. There are some commonalities between message queues and semaphores as far as data structures are concerned.

The functional principle of messages queues is relatively simple, as Figure 5-4 shows.

message queues.

A process usually referred to as the sender generates messages and writes them to a queue, while one or more other processes (logically known as receivers) 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 queue and (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 discussed. The starting point is the appropriate ipc_ids instance of the current namespace.

Again, the internal ID numbers are formally associated with kern_ipc_perm instances, 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:

Continue reading here: Info

Was this article helpful?

0 0