问题
What is the difference between message queues and a pipe in Linux?
回答1:
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 themqd_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.
回答2:
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.
来源:https://stackoverflow.com/questions/3501458/pipe-vs-msg-queue