开发者

difference between message queue and shared memory?

I read a lot of articles about differences between message queue and shared memory. But still not clear which one is good for achiev开发者_运维百科ing good performance.

Like shared memory are suppose to be good over queues but that also has performance issue in case of synchronizing it.


Both shared memory and message queues can be used to exchange information between processes. The difference is in how they are used.

Shared memory is exactly what you'd think: it's an area of storage that can be read and written by more than one process. It provides no inherent synchronization; in other words, it's up to the programmer to ensure that one process doesn't clobber another's data. But it's efficient in terms of throughput: reading and writing are relatively fast operations.

A message queue is a one-way pipe: one process writes to the queue, and another reads the data in the order it was written until an end-of-data condition occurs. When the queue is created, the message size (bytes per message, usually fairly small) and queue length (maximum number of pending messages) are set. Access is slower than shared memory because each read/write operation is typically a single message. But the queue guarantees that each operation will either processes an entire message successfully or fail without altering the queue. So the writer can never fail after writing only a partial message, and the reader will either retrieve a complete message or nothing at all.


Message queue has inherent synchronization overhead, guarantee of safety at cost of performance. Shared memory has no safeguards - if two threads access it simultaneously, they will possibly conflict (write inconsistent data) unless you assure thread safety yourself. This may be insignificant in case minor errors are allowed (say, the data goes to analog output and some noise is acceptable), so you can skip error checking altogether, and go along with "good enough" approach at quite high performance gain. Also, shared memory allows for exchange of big pieces of data and common and persistent storage of data common to several apps saving memory storage. Message queues are for lower throughput - you can for example utilize them for safeguarding access to shared memory.


When using shared memory with consideration for possible race-conditions where one process writes to it and another reads from it, something to bear in mind. There is an associated risk of using the former, suppose two processes are using it, one to write to it, the other to read from it, the one that is writing dies due to abnormal condition, the process reading it could hang or crash.

Shared memory can be deemed as faster (low overhead, high volume of data passing) then queues. But queues on the other hand, requires high overhead (the set up for making a queue to be permanent etc) with low volume of data.

The onus with shared memory is that you have to implement synchronization in order to be thread safe. Have a look at the excellent article by Beej on IPC.

When using Queues, they are thread-safe, and not alone that, messages are held in the queue regardless of the outcome, suppose two processes are using the queue, when one process writes to it (in a form of a message) and the other process that is about to read from it gets to die or killed off due to a crash or abnormal condition under a such circumstance, that message is still in place, the other process if restarted can read from the queue, i.e. no data is lost.

That is the difference between the two.


Message Queue and shared memory are used to share data between 2 processes. Message queue requires data to be shared in specific format. Both processes must agree on this and share the messages. Kernel allows us to read entire message or read nothing for message queues. But shared memory requires part of segment is shared between 2 processes, both can do some synchronization technique and share the data between processes. Since there is no need to copy data to share to other process, shared memory is faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜