Inter-Process Communication # # Linux # pipe (pipe) - An anonymous pipe pipe

We usually a data connection to another process a process stream referred to as a "pipe" is generally used as a process output connected to an input of another process via conduit. It is the kernel of a cache on the pipeline in nature . Circular queue mechanism used by the kernel, the kernel buffer means (4K) implementation.

  • Unix pipeline is among the oldest form of interprocess communication;
  • We have a data connection from one process to another process stream is called a "pipeline";
  • Pipeline is the essence of a block of memory provided by the operating system;

Each pipe as only one page buffer, the page is in the manner of a ring buffer used. This access method is a typical "producer - consumer" model. When the "producer" process a large amount of data needs to be written, and filled every time a page on the need for sleep to wait, wait for the "consumer" go read some data from the pipe, its free some space. Accordingly, if the pipeline is not readable data, "consumer" process must wait to sleep, the specific process is as shown below:

Pipeline is a kernel buffer management, the equivalent of a piece of paper into our memory. A connection process of the output end of the pipe. This process will put the information into the pipeline. The other end of the pipe is connected to a process input, the process information is taken into the duct. It does not require a large buffer (kernel buffer (4K)), which is designed to be annular structure data , so that the pipe can be recycled. When the pipeline is no information, then read from the process piping will wait until the other end of the process into information. When the pipe is filled with information, try placing the information in the process will wait until the other end of the process out information. When the two processes are terminated, the pipeline automatically disappears.

Anonymous pipes

Anonymous pipe is based on the communication file descriptor. Must be achieved through communication between two processes fork to create a child process, communication between parent and child.

Creating a single unnamed pipe, and without any real significance. We generally are in a process after creating a pipeline by the pipe (), and then by the general fork a child process, and then realize the communication between parent and child through a pipe (and therefore not difficult to launch, as long as the two processes in the presence of kinship, here refers to a genetic relationship have a common ancestor, the pipe may be employed to communicate manner).

  • 1. The parent process calls function to create pipe conduit, get two file descriptor fd [0], fd [1] read and write terminal end point of the pipe.
  • 2. The parent process calls fork to create a child process, the child process also has two file descriptors point to the same pipe.
  • 3. The parent process close the read end of the pipe, child process close the write end of the pipe. The parent process can write data to the pipe, the child will read data pipeline. Because the pipeline is implemented using a circular queue, the write data from the end of the inflow conduit, read from the outflow end, so to achieve inter-process communication.

Anonymous pipes write rules:

  • When there is no data to be read: O_NONBLOCK disable: read call blocks that process be suspended, until the data come so far; O_NONBLOCK enable: read call returns -1, errno is EAGAIN; (based on the return value may be different versions of linux kernel differences, Linux5.4.6 return value -EAGAIN)
  • When the pipeline is full time: O_NONBLOCK disable: write call blocks until the process reads the data; O_NONBLOCK enable: call returns -1, error value EAGAIN; (may vary based on different versions of the linux kernel return value, Linux5.4.6 return value -EAGAIN)
  • If all file descriptor corresponding to the write end of the pipe is closed, the read returns 0; (return value may differ based on different versions of the linux kernel, Linux5.4.6 Returns the size in bytes of the currently fetched )
  • If all of the file descriptor corresponding to the read end of the pipe is closed, the write operation SIGPIPE signal is generated, and thus may result in write process exits; (may vary based on different versions of the linux kernel return value, Linux5.4.6 -EPIPE return value )
  • When the amount of data to be written is not larger than PIPE_BUF, linux will ensure atomicity of write;
  • When data to be written is greater than PIPE_BUF, linux will not guarantee the atomicity of write;


Anonymous pipe Features:

  • Can only be used to process (the process has genetic relationship) have a common ancestor between communication; typically, a pipe is created by a process, then the process calls fork, after which you can apply the pipeline between parent and child.
  • Conduit providing streaming services (data pipeline transmitted unformatted, which requires the pipeline read and the write side party prior arrangements well format data, such as how many bytes of a message count, etc.).
  • Data is written to the pipeline following the FIFO rule.
  • In general, the process of withdrawal, the release of the pipeline, so the life cycle of the pipeline with the process.
  • In general, the kernel will synchronize pipeline operation and mutual exclusion (filled not write, do not read reading).
  • Pipe is half-duplex (again into the out side), data can only flow in one direction; communicating parties when required, the need for two pipes.
     
Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/103730956