Are message queues obsolete in linux?

后端 未结 4 825
渐次进展
渐次进展 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.

    0 讨论(0)
  • 2021-01-29 19:56

    Yes, I think that message queues are appropriate for some applications. POSIX message queues provide a nicer interface, in particular, you get to give your queues names rather than IDs, which is very useful for fault diagnosis (makes it easier to see which is which).

    Linux allows you to mount the posix message queues as a filesystem and see them with "ls", delete them with "rm" which is quite handy too (System V depends on the clunky "ipcs" and "ipcrm" commands)

    0 讨论(0)
  • 2021-01-29 19:59

    I haven't actually used POSIX message queues because I always want to leave open the option to distribute my messages across a network. With that in mind, you might look at a more robust message-passing interface like zeromq or something that implements AMQP.

    One of the nice things about 0mq is that when used from the same process space in a multithreaded app, it uses a lockless zero-copy mechanism that is quite fast. Still, you can use the same interface to pass messages over a network as well.

    0 讨论(0)
  • 2021-01-29 20:09

    Personally I am quite fond of message queues and think they are arguably the most under-utilized IPC in the unix world. They are fast and easy to use.

    A couple of thoughts:

    • Some of this is just fashion. Old things become new again. Add a shiny do-dad on message queues and they may be next year's newest and hottest thing. Look at Google's Chrome using separate processes instead of threads for its tabs. Suddenly people are thrilled that when one tab locks up it doesn't bring down the entire browser.

    • Shared memory has something of a He-man halo about it. You're not a "real" programmer if you aren't squeezing that last cycle out of the machine and MQs are marginally less efficient. For many, if not most apps, it is utter nonsense but sometimes it is hard to break a mindset once it takes hold.

    • MQs really aren't appropriate for applications with unbounded data. Stream oriented mechanisms like pipes or sockets are just easier to use for that.

    • The System V variants really have fallen out of favor. As a general rule go with POSIX versions of IPC when you can.

    0 讨论(0)
提交回复
热议问题