next up previous contents
Next: Semaphores Up: System V IPC Mechanisms Previous: System V IPC Mechanisms

Message Queues

Message queues allow one or more processes to write messages which 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 which fully describes the message queue. When message queues are created a new msqid_ds  data structure is allocate from system memory and inserted into the vector.

   figure3589
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 message 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 processes 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 called to select a new process to run. It will be woken up when one or messages have been read from this message queue.

Reading from the queue is a similar process. Again, the processes 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 it may 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 woken up and run again.


next up previous contents
Next: Semaphores Up: System V IPC Mechanisms Previous: System V IPC Mechanisms

David A. Rusling
david.rusling@reo.mts.dec.com