POSIX defines a set of nonblocking message-passing facilities known as message queues.
Like pipes, message queues are named objects that operate with readers and writers. As a priority queue of discrete messages, a message queue has more structure than a pipe and offers applications more control over communications. POSIX message queues provide a familiar interface for many realtime programmers, in that they're similar to the mailboxes found in many realtime executives.
For more information about these implementations, see the Utilities Reference and the POSIX Message Queues: Two Implementations technote.
There's a fundamental difference between QNX Neutrino native messages and POSIX message queues: our messages block—they copy their data directly between the address spaces of the processes sending the messages. POSIX message queues, on the other hand, implement a store-and-forward design in which the sender need not block and may have many outstanding messages queued. POSIX message queues exist independently of the processes that use them. You could use message queues in a design where a number of named queues will be operated on by a variety of processes over time.
Which should you use? Message queues, being defined by POSIX, are more portable, but native messages have several advantages:
Message queues resemble files, at least as far as their interface is concerned. POSIX defines the following functions that you can use to manage message queues (see the QNX Neutrino C Library Reference for more information):
Function | Description |
---|---|
mq_open() | Open a message queue |
mq_close() | Close a message queue |
mq_unlink() | Remove a message queue |
mq_send() | Add a message to the message queue |
mq_receive() | Receive a message from the message queue |
mq_notify() | Tell the calling process that a message is available on a message queue |
mq_setattr() | Set message queue attributes |
mq_getattr() | Get message queue attributes |
For strict POSIX conformance, you should create message queues that start with a single slash (/) and contain no other slashes. But note that we extend the POSIX standard by supporting pathnames that may contain multiple slashes. This allows, for example, a company to place all its message queues under its company name and distribute a product with increased confidence that a queue name will not conflict with that of another company.
In QNX Neutrino, all message queues created appear in the filename space under the directory:
For example, with the traditional implementation:
mq_open() name: | Pathname of message queue: |
---|---|
/data | /dev/mqueue/data |
/acme/data | /dev/mqueue/acme/data |
/qnx/data | /dev/mqueue/qnx/data |
You can display all message queues in the system using the ls command as follows:
ls -Rl /dev/mqueue
The size printed is the number of messages waiting.