I\'ve been playing with message queues (System V, but POSIX should be ok too) in Linux recently and they seem perfect for my application, but after reading The Art of Unix Progr
Biggest disadvantages of POSIX message queue:
select()
.(It works with select()
in Linux but not in Qnx system)Unix Datagram socket does the same task of POSIX message queue. And Unix Datagram socket works in socket layer. It is possible to use it with select()
/poll()
or other IO-wait methods. Using select()
/poll()
has the advantage when designing event-based system. It is possible to avoid busy loop in that way.
There is surprise in message queue. Think about mq_notify()
. It is used to get receive-event. It sounds like we can notify something about the message queue. But it is actually registering for notification instead of notifying anything.
More surprise about mq_notify()
is that it has to be called after every mq_receive()
, which may cause a race-condition(when some other process/thread call mq_send()
between the call of mq_receive()
and mq_notify()
).
And it has a whole set of mq_open, mq_send(), mq_receive() and mq_close()
with their own definition which is redundant and in some case inconsistent with socket open(),send(),recv() and close()
method specification.
I do not think message queue should be used for synchronization. eventfd
and signalfd
are suitable for that.
But it(POSIX message queue) has some realtime support. It has priority features.
Messages are placed on the queue in decreasing order of priority, with newer messages of the same priority being placed after older messages with the same priority.
But this priority is also available for socket as out-of-band data !
Finally, to me , POSIX message queue is a legacy API. I always prefer Unix Datagram socket instead of POSIX message queue as long as the real-time features are not needed.