Pipe Introduction
Pipelines are a way of interprocess communication in Linux, which connects the output of a program directly to the input of another program (in fact, I prefer to liken the pipe to the pipe of a rural land). The main pipeline of Linux includes two kinds: nameless pipe and famous pipe. This section mainly talks about the nameless pipeline, first introduces these two pipes. (the feature is very important!) )
1, Nameless pipe
The nameless pipe is an original method of piping communication in Linux, as shown in Figure one (left), which has the following characteristics:
① It can only be used for communication between relational processes (that is, between a parent-child process or a sibling process);
② It is a Half-duplex communication mode, with fixed read-end and write-end;
The ③ pipeline can also be viewed as a special kind of file, and it can also use the normal read () and write () functions for its reading and writing. However, it is not a normal file and does not belong to any other file system and exists only in memory.
2, famous pipeline (FIFO)
A well-known pipeline is an improvement to a nameless pipe, as shown in Figure 1 (right), which has the following characteristics:
① it can communicate with each other between two processes that are unrelated to each other;
② the pipe can be indicated by a pathname and is visible in the file system. After the pipeline is established, two processes can read and write as normal files, which is very convenient to use.
③fifo strictly follow the advanced first out rules, to the pipeline and FIFO read always return data from the beginning, write to them is to add the data to the end, they do not support such as Lseek () and other file positioning operations.
Nameless pipes and their system calls
1, pipe creation and piping instructions
Pipelines are the means of communication based on file descriptors, when a pipe is established, it creates two file descriptors fd[0] and fd[1], where fd[0] is fixed for the read pipe, and fd[1 is fixed for the write pipe, as shown in Figure 2, which constitutes a half-duplex channel.
When the pipe is closed, you can close the two file descriptors, using the normal close () function to turn off individual file descriptors.
2. Pipe creation function
Creating a pipe can be implemented by calling pipe (), as shown in the following table