14

I am working on a message queue used to communication among process on embedded Linux. I am wondering why I'm not using the message queues provided by Linux as following:

msgctl, msgget msgrcv, msgsnd.

instead of creating shared memory, and sync up with semaphore?

What's the disadvantage of using this set of functions directly on a business embedded product?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Joe.Z
  • 2,725
  • 4
  • 25
  • 30

5 Answers5

10

The functions msgctl(), msgget(), msgrcv(), and msgsnd() are the 'System V IPC' message queue functions. They'll work for you, but they're fairly heavy-weight. They are standardized by POSIX.

POSIX also provides a more modern set of functions, mq_close(), mq_getattr(), mq_notify(), mq_open(), mq_receive(), mq_send(), mq_setattr(), and mq_unlink() which might be better for you (such an embarrassment of riches).

However, you will need to check which, if either, is installed on your target platforms by default. Especially in an embedded system, it could be that you have to configure them, or even get them installed because they aren't there by default (and the same might be true of shared memory and semaphores).

The primary advantage of either set of message facilities is that they are pre-debugged (probably) and therefore have concurrency issues already resolved - whereas if you're going to do it for yourself with shared memory and semaphores, you've got a lot of work to do to get to the same level of functionality.

So, (re)use when you can. If it is an option, use one of the two message queue systems rather than reinvent your own. If you eventually find that there is a performance bottleneck or something similar, then you can investigate writing your own alternatives, but until then — reuse!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • But why not just use sockets? Those are pre-debugged, too, etc etc. Sure, maybe you want AF_UNIX and SOCK_SEQPACKET type semantics, but sockets sure do seem a lot better to me. – tchrist Mar 01 '13 at 18:32
  • @tchrist: care to elaborate on why sockets seem better to you? – Gauthier Dec 16 '14 at 15:00
6

System V message queues (the ones manipulated by the msg* system calls) have a lot of weird quirks and gotchas. For new code, I'd strongly recommend using UNIX domain sockets.

That being said, I'd also strongly recommend message-passing IPC over shared-memory schemes. Shared memory is much easier to get wrong, and tends to go wrong much more catastrophically.

2

Message passing is great for small data chunks and where immutability needs to be maintained, as message queues copy data.

A shared memory area does not copy data on send/receive and can be more efficient for larger data sets at the tradeoff of a less clean programming model.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
  • 1
    Actually even for large data chunks, shared memory alone can't be a good idea. Storing the data in a common memory and instead of using semaphore or mutex to access that memory, using ipc will be good idea for less error prone programming. – theB Mar 05 '12 at 07:23
  • @theB: just to be sure I understand, for large data, you'd still use `shm`, but not lock it with `sem`? You'd write to the `shm`, then pass an ipc message to announce that data is ready? – Gauthier Dec 16 '14 at 15:03
1

The disadvantages message queues are miniscule - some system call and copying overhead - which amount to nothing for most applications. The benefits far outweigh that overhead. Synchronization is automatic and they can be used in a variety of ways: blocking, non-blocking, and since in linux the message queue types are implemented as file descriptors they can even be used in select() calls for multiplexing. In the POSIX variety, which you should be using unless you have a really compelling need to use SYSV queues, you can even automatically generate threads or signals to process the queue items. And best of all they are fully debugged.

Duck
  • 26,924
  • 5
  • 64
  • 92
0

Message queue and shared memory are different. And it is upto the programmer and his requirement to select which to use. In shared memory you have to be bit careful in reading and writing. And the processes should be synchronized. So the order of execution is very important in shared memory. In shared memory, there is no way to find whether the reading value is newly written value or the older one. And there is no explicit mechanism to wait.

Message queue and shared memory are different. And it is upto the programmer and his requirement to select which to use. There are predefined functions to make your life easy in message queue.

theB
  • 2,048
  • 2
  • 20
  • 29