What is a pipe
The pipeline is one of the original UNIX IPC forms supported by Linux and has the following features:
The pipe is half-duplex and the data flows only in one direction; two pipelines need to be established for both sides of the communication, only for parent-child processes or sibling processes (related processes), and for separate file systems: Pipelines are a file for processes at both ends of the pipeline, but it is not a normal file , it does not belong to some kind of file system, but rather, it is a separate file system and only exists in memory. Read and write data: a process that writes to the pipe is read by the process on the other side of the pipe. The written content is added to the end of the pipe buffer each time, and data is read from the head of the buffer each time.
In Linux, pipelines are a very frequent communication mechanism. In essence, a pipe is a file, but it is different from the general file, the pipeline can overcome the use of files to communicate the two issues, the specific performance:
Limit the size of the pipe. In fact, a pipe is a fixed size buffer. In Linux, the size of the buffer is 1 pages, or 4K bytes, so that it doesn't grow as quickly as a file. Using a single fixed buffer can also be problematic, such as when writing a pipe, and when this happens, subsequent write () calls to the pipe are blocked by default, waiting for some data to be read so that enough space is available for write () calls to be written.
The read process may also work faster than the write process. The pipe becomes empty when all current process data has been read. When this happens, a subsequent read () call is blocked by default, waiting for some data to be written, which resolves the problem that the read () call returns to the end of the file.
Note: Reading data from a pipe is a one-time operation, and once the data is read, it is discarded from the pipeline, freeing up space to write more data.
Implementation mechanism:
A pipe is a buffer that is managed by the kernel, which is the equivalent of a note that we put into memory. One end of the pipe connects the output of a process. This process puts information into the pipeline. The other end of the pipe is connected to the input of a process that takes out the information that is put into the pipe. A buffer does not need to be large, it is designed to be a circular data structure so that the pipeline can be recycled. When there is no information in the pipeline, the process read from the pipe waits until the process in the other end puts the information. When the pipe is filled with information, the process of trying to put the information waits until the process on the other end takes out the information. When two processes are terminated, the pipeline automatically disappears.
In principle, pipelines are built using fork mechanisms so that two processes can be connected to the same pipe. At the very beginning, the top two arrows are connected to the same process Process1 (two arrows attached to the Process1). When fork the replication process, the two connections are also copied to the new process (PROCESS2). Each process then closes a connection that it does not need (two yellow arrows are turned off; Process1 closes the input connection from the pipe, Process2 turns off the connection to the pipe), so the remaining black connections make up the pipe as shown above.
Implementation Details:
In Linux, the implementation of the pipeline does not use a specialized data structure, but it uses the file structure of the filesystem and the index node inode of the VFS. This is achieved by pointing the two file structure to the same temporary VFS index node, which points to a physical page. The following figure
There are two file data structures, but they define file action routine addresses differently, one is the routine address that writes data to the pipeline, and the other is a routine address that reads data from the pipe. In this way, the user program's system call is still the usual file operation, but the kernel uses this abstract mechanism to implement the special operation of the pipeline.
Determine the capacity of the current computer pipeline
Read end not read, write end always write
https://www.ibm.com/developerworks/cn/linux/l-ipc/part1/