Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe vs msg queue

Tags:

What is the difference between message queues and a pipe in Linux?

like image 357
mint9 Avatar asked Aug 17 '10 10:08

mint9


People also ask

What is the difference between pipe and FIFO?

The pipe has no name; it is created for one use and both ends must be inherited from the single process which created the pipe. A FIFO special file is similar to a pipe, but instead of being an anonymous, temporary connection, a FIFO has a name or names like any other file.

What is the difference between message queue and shared memory?

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.

Are pipes shared memory or message passing?

It's message passing. You specify a buffer to write into the socket buffer, and you find out how much space it has available beforehand using getTxAvailable() or whatever. It's not really shared memory as it does a buffer blit operation to help encapsulation of the socket.

What is difference between pipes and named pipes?

A traditional pipe is “unnamed” and lasts only as long as the process. A named pipe, however, can last as long as the system is up, beyond the life of the process. It can be deleted if no longer used. Usually a named pipe appears as a file and generally processes attach to it for inter-process communication.


2 Answers

Off the top of my head and assuming you talk about posix message queues (not the SysV ones):

  • Pipes aren't limited in size, message queues are.
  • Pipes can be integrated in systems using file descriptors, message queues have their own set of functions, though linux supports select(), poll(), epoll() and friends on the mqd_t.
  • Pipes, once closed, require some amount of cooperation on both sides to reestablish them, message queues can be closed and reopened on either side without the coorporation of the other side.
  • Pipes are flat, much like a stream, to impose a message structure you would have to implement a protocol on both sides, message queues are message oriented already, no care has to be taken to get, say, the fifth message in the queue.
like image 142
hroptatyr Avatar answered Oct 04 '22 14:10

hroptatyr


They are very different things, really.

The biggest practical difference is that a pipe doesn't have the notion of "messages", it's just a pipe to write() bytes to and read() bytes from. The receiving end must have a way to know what piece of data constitute a "message" in your program, and you must implement that yourself. Furthermore the order of bytes is defined: bytes will come out in the order you put them in. And, generally speaking, it has one input and one output.

A message queue is used to transfer "messages", which have a type and size. So the receiving end can just wait for one "message" with a certain type, and you don't have to worry if this is complete or not. Several processes may send to and receive from the same queue.

see man mq_overview and/or man svipc for more information.

like image 24
mvds Avatar answered Oct 04 '22 14:10

mvds