![]() |
|
||||
POSIX 1.b Migration Guide |
Message Queues
The message queue interface has changed extensively. A number of Draft 9 features are completely eliminated in favor of performance improvements and ease of use in POSIX.1b. The following is a list of Draft 9 functions with no equivalent in POSIX.1b; applications that depend on these functions may not migrate easily to POSIX.1b:
Some of the functions above may be partially simulated by other functions in POSIX.1b. For example, mqgetpid() can be simulated by encoding the pid of the process in the message itself. The mqpurge() function can be simulated with mq_receive() in a while loop until it fails. The mqputevt() and mqgetevt() functions can be simulated with the sigqueue() and sigwaitinfo() functions from POSIX.1b, respectively. The msgalloc() and msgfree() functions have no corresponding functionality.
Three other features are no longer supported: asynchronous message sending and receiving; using the MSG_MOVE and MSG_USE flags; and selective removing messages from somewhere other than the head of the queue.
There are other important changes; Draft 9 and POSIX.1b differ with respect to persistence and how the names for message queues are implemented. This is similar to the differences for named semaphores as described in Chapter 7, "Semaphores" on page 67.
A comparison of the various message queue features is provided later in
this chapter.
Creating Message Queues
POSIX.1b message queues are created with the mq_open() function and the O_CREAT flag as opposed to the mkmq() function from Draft 9, as per the following example:
Draft 9 Code
Equivalent POSIX.1b Code
Data Structure Changes
The two data structures, msgcb and mqstatus, from Draft 9 were combined into a single data structure, mq_attr, in POSIX.1b. Also, a number of entries from both of these structures were eliminated. For example, the time stamp and event fields from msgcb have no equivalent in POSIX.1b.
The message length and message data fields from msgcb are now separate arguments for the message sending and receiving functions. A comparison of data structures is provided later in this chapter in "Data Structures".
Getting and Setting Message Queue Attributes
The following example illustrates the comparison of the interfaces to get and set message queue attributes. The mq_maxmsg and mq_msgsize attributes for a POSIX.1b message queue may only be set at creation time. "Changes from Draft 9 to POSIX.1b" tabulates the restrictions on setting attributes for a POSIX.1b message queue.
The default Message Queue attributes are as follows:
Draft 9 Code
Equivalent POSIX.1b Code
Sending and Receiving Messages
The following example compares the sending and receiving of messages between message queue facilities. The fork() call is only relevant to illustrate how messages may be sent and received between two processes.
Draft 9 Code
Equivalent POSIX.1b Code
Notification of Message Availability
The new mq_notify() function in POSIX.1b is used to notify a process that a message is available on a message queue. This is done by sending a signal to the process when the message queue changes from empty to non-empty as illustrated in the following example.
When a notification request is attached to a message queue, another process may be blocked in mq_receive() waiting to receive a message. If a message arrives at the queue, mq_receive() is completed and the notification request remains pending. If there is no process blocked in mq_receive(), the specified signal handler is called.
The next example uses the flag no_msg to ensure that the notification request is satisfied, and that the same message is received with an mq_receive() call: The sa_flags flag is set to SA_SIGINFO, and the sigev_notify field is set to SIGEV_SIGNAL to ensure the use of a real-time signal:
Changes from Draft 9 to POSIX.1b
Interface Changes
Message queues have changed in a fairly major way. A number of facilities from Draft 9 are no longer available; however, POSIX.1b offers a new facility for notification of message availability. The new implementation offers better performance. Speeds comparable to raw memory copy are attainable using the new, simple POSIX.1b functions
Message Queue Interface Message queue = special file Independent of file system Persistent as well as non-persistent message queues with the MQ_PERSIST flag Persistent message queues Queue wrapping with MQWRAP No queue wrapping Buffer management with MSG_COPY, MSG_USE, MSG_MOVE All messages copied Message transfer synchronization control with MSG_WAIT, MSG_ASYNC, MSG_NOWAIT No message synchronization Truncation control with MSG_TRUNC Overlong messages rejected at the time
of sending Ability to send an event via a
message queue No event sending Selective message receive order No equivalent1 Done with mq_open() No Equivalent No Equivalent No Equivalent No Equivalent No Equivalent No Equivalent No Equivalent Notify process that a message is available on a queue. Combines the flags of msgcb and mqstatus.
1LynxOS provides the mq_selective_receive() function to support this behavior, even though it is not in the POSIX.1b standard.
Data Structures
The msgcb and mqstatus structures are combined into the single mq_attr structure in POSIX.1b.
Attributes
There are various restrictions on setting attributes for message queues from POSIX.1b. The table below shows the attributes that may be set, and when. All attributes may be queried at any time.
Message Queue Attributes Yes, any time after creation Yes, only at creation Yes, only at creation No
Messages
Message Priorities
POSIX.1b provides a new concept of message priorities. A message is inserted into the queue, and received from the queue according to message priority. This priority is independent of the process priority.
Selective Receive
Draft 9 message queues allow the application to selectively remove queued messages by type. This facility has been replaced by the message priority facility described above. Note that priorities are somewhat less flexible than message typing, because only the highest priority message is retrievable. LynuxWorks has provided a proprietary extension, mq_selective_receive, to retain
this functionality.
Process Priorities
Process priorities come into the picture when sending and receiving messages. If more than one process is blocked while sending to a full message queue (or receiving from an empty message queue) with priority scheduling, the highest-priority process, which has been waiting the longest, is unblocked first.
Synchronization Control
With Draft 9 message queues, it is possible to control how processes waited for each other by specifying the MSG_WAIT, MSG_ASYNC, and MSG_NOWAIT flags on a per-message basis. Such options are not available with POSIX.1b message queues. All messages in POSIX.1b are sent and received with behavior equivalent to the Draft 9 MSG_NOWAIT.
Buffer Management
With Draft 9 message queues, it is possible to control the use of buffers to achieve higher performance with the MSG_MOVE, MSG_USE, and MSG_COPY flags on a per-message basis. This feature is discontinued in POSIX.1b. All POSIX.1b message queues have behavior equivalent to the MSG_COPY flag from Draft 9.
Sending and Receiving Events
Draft 9 allows events to be sent along message queues. POSIX.1b does not provide this capability.
Purging, Data Buffer Allocation/Freeing
Draft 9 provides the following functions:
The mqpurge() function can be simulated with mq_receive() in a while loop until it fails. The msgalloc() and msgfree() functions have no
corresponding functionality.
Sender ID
The mqgetpid() function from Draft 9 allowed a process to determine the pid of the sender process. There is no equivalent function for this feature in POSIX.1b. However, the user can work around this by encoding the pid in the
message itself.
Queue Wrap
With Draft 9 message queues, it is possible to specify the queue wrap behavior, so that older messages could be overwritten by newer ones as messages were sent to a full queue. This behavior was requested with the MQWRAP flag at message queue creation time. POSIX.1b does not support this capability.
Time-Stamping
It is possible to time-stamp Draft 9 messages. This feature is not supported by POSIX.1b. An application writer can work around this by encoding the time-stamp in the message.
Truncation Control
Draft 9 provides a MSG_TRUNC flag to truncate a message when receiving, if it is larger than the buffer. POSIX.1b does not allow a message to be sent if its length exceeds the message size of the queue, and does not allow mq_receive to be called with a buffer size smaller than the message size.
A Pointer-Worth of Data
Draft 9 provides a MSG_OVERRIDE flag to indicate receipt of a pointer-worth of data. POSIX.1b does not provide special support for such functionality, although
4-byte-long messages are supported.
Notification of Message Availability
POSIX.1b provides the new function, mq_notify(), to notify a process when a message queue changes from empty to non-empty.
exec() Behavior
With Draft 9, message queue file descriptors of a process remain open after exec(), except if the FD_CLOEXEC flag was set. With POSIX.1b, open message queue descriptors of a calling process are closed upon exec().
New Utilities
LynxOS provides two new utilities, lipcs and lipcrm, to list and remove message queues (and other POSIX.1b IPC facilities), respectively; refer to the lipcs and lipcrm man pages for more information.
Interoperability
Draft 9 message queues and POSIX.1b message queues are distinct. There is no interoperability. For example, it is not possible to send a message on a Draft 9 queue and receive it on a POSIX.1b message queue.
![]() LynuxWorks, Inc. 855 Branham Lane East San Jose, CA 95138 http://www.lynuxworks.com 1.800.255.5969 |
![]() |
![]() |
![]() |
![]() |