Are message queues obsolete in linux?

后端 未结 4 821
渐次进展
渐次进展 2021-01-29 19:38

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

4条回答
  •  醉梦人生
    2021-01-29 19:48

    Biggest disadvantages of POSIX message queue:

    • POSIX message queue does not make it a requirement to be compatible with select().(It works with select() in Linux but not in Qnx system)
    • It has surprises.

    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.

提交回复
热议问题