This chapter describes POSIX message queues, which allow processes to exchange data in the form of messages. POSIX message queues are similar to their System V counterparts, in that data is exchanged in units of whole messages. However, there are also some notable differences:
POSIX message queues are reference counted. A queue that is marked for deletion is removed only after it is closed by all processes that are currently using it.
Each System V message has an integer type, and messages can be selected in a variety of ways using msgrcv(). By contrast, POSIX messages have an associated priority, and messages are always strictly queued (and thus received) in priority order.
POSIX message queues provide a feature that allows a process to be asynchronously notified when a message is available on a queue.
POSIX message queues are a relatively recent addition to Linux. The required implementation support was added in kernel 2.6.6 (in addition, glibc 2.3.4 or later is required).
POSIX message queue support is an optional kernel component that is configured via the CONFIG_POSIX_MQUEUE
option.
The main functions in the POSIX message queue API are the following:
The mq_open() function creates a new message queue or opens an existing queue, returning a message queue descriptor for use in later calls.
The mq_close() function closes a message queue that the process previously opened.
The mq_unlink() function removes a message queue name and marks the queue for deletion when all processes have closed it.
The above functions all serve fairly obvious purposes. In addition, a couple of features are peculiar to the POSIX message queue API:
Each message queue has an associated set of attributes. Some of these attributes can be set when the queue is created or opened using mq_open(). Two functions are provided to retrieve and change queue attributes: mq_getattr() and mq_setattr().
The mq_notify() function allows a process to register for message notification from a queue. After registering, the process is notified of the availability of a message by delivery of a signal or by the invocation of a function in a separate thread.