A, reference
Unix kernel source code analysis
Two, what is a pipeline
A pipeline is a mechanism for communication between parent and child processes, because processes have separate virtual address Spaces and no process can directly access data owned by another process. Pipelines are designed to achieve interprocess communication.
A pipe makes clever use of a part of the file system to make interprocess communication possible by first acquiring the inode of the root disk and then exchanging data with the storage area to which the inode points. This file (inode and storage area) constitutes the entity of the pipeline. The pipe capacity is defined by PIPSIZE in Ken /pipe.c
The communication process using a pipe is as follows: (1) the sending process writes data to the pipe until the pipe is full; (2) a process that switches to the receiver, so that data is read in from the pipe and data that has been received is deleted from the pipe; (3) After all data is read, switch to the process of sending and sending, and return the processing of (1)
The sender takes the address +1 of the inode[] element corresponding to the pipe, and the receiver takes the address +2 as the parameter to perform sleep().
Advantages of using pipes:
Passing data between processes can also be done using temporary files. The advantages of using a pipe over temporary files are: (1) The resource of the available block device is limited. The capacity of the pipe is a fixed 4096 bytes. Therefore, it will not occupy more block device area even if it is used to exchange larger data capacity. Take up with the exchange of data capacity equal area of the block device (2) pipe need buffer capacity is less than the total capacity of the buffer, to play a piece of equipment of the buffer cache function, and, when after the sender to write data into the process of the pipeline, the process of the receiver will immediately read and block device cache effect will be more apparent, when using temporary files, If you need to output a file larger than the size of the device buffer, the caching effect of the buffer will be affected. However, when a pipe is used and the executing process is switched from the sender to the receiver, you cannot expect the caching effect of the buffer if there is a process with higher execution priority that also uses the buffer of the block device. At this point, although the performance is not improved, there is no impact on the communication content itself because the data has been output to the block device.
Pipeline is based on the existing file system, the cost of implementation is low, although occupy less resources, but it realizes the high-speed communication between processes, low investment and high output can be regarded as the biggest charm of the pipeline.
Three, start pipeline communication
The system call pipe is used to establish pipe communication, and pipe() is the handler for the system call
User. u_ofile[] and File [] first allocate the elements for read and write, then get the inode[] element of the root disk, point the file[] element for read and write to the inode[] element, Sets the FPIPE flag bit representing the pipe for the File [] element
The file descriptor used by read and write is returned to the user program, and the user program can read and write the file descriptor just like a normal file to achieve pipe communication.
4. Data receiving and receiving
For the file descriptor obtained through pipe system call, system calls read and write can be executed just as for ordinary files, so as to realize data transceiver
When the FPIPE flag bit of the File [] element is set, the writep() and readp() methods are executed in the RDWR () method
4.1 writep
Writep () is used to write to the pipeline. Since the entity of the pipeline is a file, writei() is called to write data in the same way as that of ordinary files. When the pipeline is filled with (4096 bytes), it enters the sleep state. However, unlike normal file processing, the file offset file.f_offset does not change