📜  管道和消息队列的区别

📅  最后修改于: 2021-09-13 03:11:28             🧑  作者: Mango

管道:
Unix 系统使用管道来建立进程间通信。管道提供单向数据流。管道是使用 pipe()函数创建的。

句法:

#include int pipe(int fd);  

pipe()函数返回两个文件描述符,fd[O] 和 fd[1]。 fd[0] 为读取而打开,fd[1] 为写入管道而打开。数据从管道的一端流向另一端。管道函数在成功时返回“0”或在错误时返回 -1。

消息队列:
一个消息队列,允许一个或多个进程写入消息以供其他进程读取。消息队列被实现为消息的链接列表,并存储在内核中。每个消息队列由消息队列标识符标识。内核会跟踪系统中创建的所有消息队列。

管道和消息队列的区别:

S.NO Pipes Message Queues
1. Pipe is a form of Unix IPC that provides flow of data in one direction. Message queues is a form of system VIPC that store a linked list of messages
2. Creating a pipe using pipe() function, returns two file descriptors, one for reading another for writing. Creating a message queues using msgget() function returns a message queue identifier.
3. Pipes and FIFOs are unidirectional, i.e., the data can flow in one direction only. Message queues are bidirectional i.e., the data can flow in both directions.
4. With Pipes and FIFOs, the data must be fetched in first in first out order. With message queues the messages can be read in any order that is consistent with the values associ ated with the message types.
5. Priorities can’t be assigned to the messages. Priorities can assigned to the messages by associ ating a priority to a type or range of types.
6. With Pipes and FIFOs, there must be some process waiting for a message to be written over the pipes and FIFOs i.e., both a reader process and a writer must exist. With message queues a process can write the messages to a queue then exit, so that the messages can be read by another process at a later time.
7. Pipes are completely deleted from the system, when the last process having reference to it terminates. Message queue and its contents remain in the system on process termination until they are specifically read or deleted by some process calling mcgregor magento, by executing the ipcrm(1) command or by rebooting the system.
8. The maximum number of bytes that can be written to a pipe of FIFO is 4096 bytes. The maximum message size that can be written to a message queue is 8192 bytes.
9. A major advantage of using named pipes is that they provide a useful way to send one-line requests to an OpenEdge background session running a message handler procedure better Performance. Message queues enable asynchronous communication, which means that the endpoints that are producing and consuming messages interact with the queue, not each other.
10. Multiple users can send requests through the same named pipe and each request is removed from the pipe as it is received. Increased Reliability.