The relationship between a message queue descriptor and an open message queue is analogous to the relationship between a file descriptor and an open file (Figure 5-2, in Relationship Between File Descriptors and Open Files). A message queue descriptor is a per-process handle that refers to an entry in the system-wide table of open message queue descriptions, and this entry in turn refers to a message queue object. This relationship is illustrated in Figure 52-1.
On Linux, POSIX message queues are implemented as i-nodes in a virtual file system, and message queue descriptors and open message queue descriptions are implemented as file descriptors and open file descriptions, respectively. However, these are implementation details that are not required by SUSv3 and don’t hold true on some other UNIX implementations. Nevertheless, we return to this point in Linux-Specific Features, because Linux provides some nonstandard features that are made possible by this implementation.
Figure 52-1 helps clarify a number of details of the use of message queue descriptors (all of which are analogous to the use to file descriptors):
An open message queue description has an associated set of flags. SUSv3 specifies only one such flag, O_NONBLOCK
, which determines whether I/O is nonblocking.
Two processes can hold message queue descriptors (descriptor x in the diagram) that refer to the same open message queue description. This can occur because a process opens a message queue and then calls fork(). These descriptors share the state of the O_NONBLOCK
flag.
Two processes can hold open message queue descriptors that refer to different message queue descriptions that refer to the same message queue (e.g., descriptor z in process A and descriptor y in process B both refer to /mq-r
). This occurs because the two processes each used mq_open() to open the same queue.