Message Queues

Message Queues Message queues allow one or more processes to write messages that will be read by one or more reading processes. Linux maintains a list of message queues, the msgque vector: each element of which points to a msqid_ds data structure that fully describes the message queue. When message queues are created, a new msqid_ds data structure is allocated from system memory and inserted into the vector.

Figure: System V IPC Message Queues

Each msqid_ds data structure contains an ipc_perm data structure and pointers to the messages entered onto this queue. In addition, Linux keeps queue modification times such as the last time that this queue was written to and so on. The msqid_ds also contains two wait queues: one for the writers to the queue and one for the readers of the queue.

Each time a process attempts to write a message to the write queue, its effective user and group identifiers are compared with the mode in this queue’s ipc_perm data structure. If the process can write to the queue then the message may be copied from the process’ address space into a msg data structure and put at the end of this message queue. Each message is tagged with an application specific type, agreed between the cooperating processes. However, there may be no room for the message as Linux restricts the number and length of messages that can be written. In this case the process will be added to this message queue’s write wait queue and the scheduler will be called to select a new process to run. It will be awakened when one or more messages have been read from this message queue.

Reading from the queue is similar. Again, the process’ access rights to the write queue are checked. A reading process may choose to either get the first message in the queue regardless of its type or select messages with particular types. If no messages match this criteria the reading process will be added to the message queue’s read wait queue and the scheduler run. When a new message is written to the queue this process will be awakened and run again.