pipelines and well-known pipelines
In this series of authors, the author outlines several main methods of communication between Linux processes. Among them, the pipeline and the famous pipeline are one of the earliest inter-process communication mechanisms. The pipeline can be used for communication between related processes. The famous pipeline overcomes the limitation that the pipeline has no name. Therefore, in addition to the function of the pipeline, it allows Communication between unrelated processes. Recognizing the rules of reading and writing of pipelines and well-known pipelines is the key to applying them in the program. Based on the detailed discussion of the communication mechanism between pipelines and well-known pipelines, this paper uses examples to verify the rules of reading and writing. It helps to enhance readers' perception of the rules of reading and writing, and also provides application examples.
1. Pipeline Overview and Related API Applications
1.1 Key Concepts Related to Pipeline
Pipeline is one of the original Unix IPC forms supported by Linux. It has the following characteristics:
Pipe is half-duplex, data It can only flow in one direction; when two parties need to communicate, two pipelines need to be established;
can only be used between father and child processes or between sibling processes (processes with kinship);
constitutes an independent File system: A pipe is a file for a process at both ends of a pipe, but it is not a normal file. It does not belong to a file system. Instead, it is a self-contained portal that forms a separate file system and exists only in memory.
Data read and write: The content written by a process into the pipeline is read by the process at the other end of the pipeline. The written content is added at the end of the pipe buffer each time, and the data is read from the head of the buffer each time.
1.2 Pipeline creation:
#include int pipe(int fd[2])
The two ends of the pipe created by this function are in the middle of a process, so there is not much meaning in the actual application, therefore, a After the process creates a pipe by pipe(), it generally forks a child process and then implements communication between the parent and child processes through the pipe (so it is not difficult to launch, as long as there is a relationship between the two processes, the kinship here means Common ancestors can use the pipeline to communicate).
1.3 Pipeline read and write rules:
Both ends of the pipe can be described by the description words fd[0] and fd[1] respectively. It should be noted that the two ends of the pipe are fixed tasks. That is, one end can only be used for reading, which is represented by the description word fd[0], which is called the pipeline read end; the other end can only be used for writing, which is represented by the description word fd[1], which is called the pipeline write end. Attempting to read data from the pipe writer or writing data to the pipe reader will result in an error. General file I/O functions can be used for pipes such as close, read, write, and so on.
Reading data from the pipeline:
If the write end of the pipeline does not exist, it is considered that the end of the data has been read, and the read function returns the read byte number to 0;
When the write end of the pipeline When present, if the requested number of bytes is greater than PIPE_BUF, the number of existing data bytes in the pipeline is returned. If the requested number of bytes is not greater than PIPE_BUF, the number of existing data bytes in the pipeline is returned (in this case, in the pipeline) The amount of data is less than the requested amount of data); or the number of bytes returned (in this case, the amount of data in the pipeline is not less than the requested amount of data). Note: (PIPE_BUF is defined in include/linux/limits.h, and different kernel versions may vary. Posix.1 requires PIPE_BUF to be at least 512 bytes and red hat 7.2 to 4096).
About the pipe read rule validation:
/************** * readtest.c * **************/#include #include
Write data to the pipeline:
When writing data to the pipeline, linux will not guarantee the atomicity of the write. If there is a free area in the pipeline buffer, the write process will try to write to the pipeline. data. If the read process does not read the data in the pipeline buffer, the write operation will block all the time.
Note: It only makes sense to write data to the pipeline when the read end of the pipeline exists. Otherwise, the process that writes data to the pipe will receive the SIFPIPE signal from the kernel, which the application can process or ignore (the default action is the application terminate).
Verification of the write rules of the pipeline 1: The dependency of the write end on the read end