管道与消息队列

发布于 2024-09-14 20:08:59 字数 28 浏览 2 评论 0原文

Linux 中的消息队列和管道有什么区别?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玉环 2024-09-21 20:08:59

我突然想到,假设您谈论的是 posix 消息队列(不是 SysV 消息队列):

  • 管道的大小不受限制,消息队列则受限制。
  • 管道可以使用文件描述符集成到系统中,消息队列有自己的一组函数,尽管linux支持select()poll()epoll()mqd_t 上的朋友。
  • 管道一旦关闭,需要双方进行一定程度的合作才能重新建立它们,消息队列可以在任何一方关闭和重新打开,而无需另一方的配合。
  • 管道是扁平的,很像流,要强加消息结构,您必须在两侧实现协议,消息队列已经是面向消息的,无需注意获取队列中的第五条消息。

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.
酷炫老祖宗 2024-09-21 20:08:59

它们确实是非常不同的东西。

最大的实际区别是管道没有“消息”的概念,它只是一个用于写入字节和读取字节的管道。接收端必须有办法知道程序中哪些数据构成“消息”,并且您必须自己实现。此外,字节的顺序是定义的:字节将按照您输入的顺序出现。而且,一般来说,它有一个输入和一个输出。

消息队列用于传输“消息”,消息具有类型和大小。因此接收端只需等待某一类型的“消息”,而不必担心这是否完整。多个进程可以向同一个队列发送数据并从同一个队列接收数据。

请参阅 man mq_overview 和/或 man svipc 了解更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文