Pipe vs msg queue
Asked Answered
H

2

20

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

Housebreak answered 17/8, 2010 at 10:25 Comment(0)
B
23

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.
Bragg answered 17/8, 2010 at 11:3 Comment(2)
ok, thanks a lot... But I have a small doubt "pipes once closed requires some kind of support on both sides", you mean to highlight the point that pipes are not kernel persistent and message queus are... And exactly what kind of suport is required to reesatblish the pipe once closed?Housebreak
@mint9: well generally speaking you need to catch the SIGPIPE, handle it gracefully, then `reopen' the pipe. I imagine you could fork() your process (on both sides), dup your stdin/stdout, keep the parents running (they act as guards), then when closed you let your children die (on both sides) and redo the fork/dup/pipe procedure.Bragg
G
14

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.

Gentleman answered 17/8, 2010 at 11:4 Comment(7)
even in queue you can send any structure as message so in this case also the receiving end must know "what piece of data constitute message in your program.Distributor
Do they really transfer messages? I looked this example on SO, and it seem to just transfer abstract characters, I don't see any messages going.Cenozoic
@Cenozoic what do you mean by “message”?Gentleman
You said a message queue is used to transfer "messages", which have a type and size, I'd expect something like this.Cenozoic
@Cenozoic the difference is that the type and length information are provided by the api - they are not part of the payload like in TLVGentleman
Okay, but in the example I linked in 1-st comment, this API is used to receive abstract characters, like read() would do. Do they use the wrong functions, and there are ones that return/receive some struct?Cenozoic
@Cenozoic it is entirely up to you how to treat the payload! Some uses may be interchangeable with the typical read() use case.Gentleman

© 2022 - 2024 — McMap. All rights reserved.