Are message queues obsolete in linux?
Asked Answered
A

5

80

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 Programming I'm not sure if they are really a good choice.

http://www.faqs.org/docs/artu/ch07s02.html#id2922148

The upper, message-passing layer of System V IPC has largely fallen out of use. The lower layer, which consists of shared memory and semaphores, still has significant applications under circumstances in which one needs to do mutual-exclusion locking and some global data sharing among processes running on the same machine. These System V shared memory facilities evolved into the POSIX shared-memory API, supported under Linux, the BSDs, MacOS X and Windows, but not classic MacOS.

http://www.faqs.org/docs/artu/ch07s03.html#id2923376

The System V IPC facilities are present in Linux and other modern Unixes. However, as they are a legacy feature, they are not exercised very often. The Linux version is still known to have bugs as of mid-2003. Nobody seems to care enough to fix them.

Are the System V message queues still buggy in more recent Linux versions? I'm not sure if the author means that POSIX message queues should be ok?

It seems that sockets are the preferred IPC for almost anything(?), but I cannot see how it would be very simple to implement message queues with sockets or something else. Or am I thinking too complexly?

I don't know if it's relevant that I'm working with embedded Linux?

Aksel answered 8/6, 2009 at 22:26 Comment(0)
L
91

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.

Laroche answered 9/6, 2009 at 0:2 Comment(2)
7 years later.. hopefully it's not too much to be still somewhat relevant: I wonder about the default settings of message queues on Ubuntu 14.04, linux 3.13, namely cat /proc/sys/fs/mqueue/msg_max lists 10 (messages in a queue) and /proc/sys/fs/mqueue/msgsize_max is 8192 (bytes) -- they are oddly small. Is there some strict reason for these defaults or they are just old? (The man mq_overview says hard limit on msg_max is about 32768, which is quite high.) I don't mean to create a infinite stream-like queue, but is 100-1000 in msg_max ok?Kafir
A little late, but: when I use POSIX queues, I have anywhere from 5-100 messages in the queue and have not experienced issues @KafirCanonicity
B
19

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)

Beaulahbeaulieu answered 9/6, 2009 at 7:2 Comment(1)
This feature is also available for UNIX Datagram socket. I can bind datagram socket to a file and receive the same way as POSIX message queue. I can even use netcat to send test data to the datagram socket file.Rolfrolfe
N
14

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.

Nealah answered 8/6, 2009 at 22:45 Comment(0)
R
14

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.

Rolfrolfe answered 8/6, 2017 at 19:27 Comment(0)
S
3

Message queues are very useful to build local decoupled applications. They are super fast, they are block organized (no need for buffering, cutting, etc which is the case for streaming sockets), basically few memcpy() operations (user code copy block to kernel, and kernel copy block to other process reading from q), and that's the story for message delivery. Some industry known middlewares such as Oracle Tuxedo or Mavimax Enduro/X uses these queues to help to build load balanced, high performance, fault tolerant decomposed, distributed applications. These queues allows to do load balancing, when several executables reads from the same queue, and kernel scheduler just distributes the message to processes which ever is idling. The nice thing for Linux is that poll can be done on Posix queues, which helps a to solve certain scenarios. For IBM AIX it is possible to do poll on System V queues.

For example, two processes can communicate easily locally over the queues with quite impressive throughput (~70k req+rply/sec):

Message sending and reply via queues / performance

If networking is needed, then for example Enduro/X provides tpbridge process which basically reads from messages from local queue, sends blocks to some other machine, where the other end injects the messages back in the local queue.

Also when comparing to sockets, you do not get any issues with queues, such as busy/lingering sockets when for example some binary have crashed, i.e. program at startup can immediately start to read the queues and do the processing.

Stroy answered 14/12, 2021 at 23:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.