Detailed linux-- pipeline

Notation

| And pipeline particular image.

effect:

    Linux conduit is very important to a communication mode, a program is output directly connected to the input of another, often said plurality of conduit means unnamed pipe, the pipe can only be used unknown genetic relationship between processes having this is the biggest difference between it and the named pipe.
A named pipe is called the named pipe or FIFO (First In First Out), you can create a function mkfifo ().

Implementation Mechanism

    In Linux, the pipeline is a very frequent communication mechanism to use. Essentially, the pipe is a file, but it is different and general files, pipes can be overcome using file two issues of communication, specific performance:
1. Limit the size of the pipe. In fact, the pipe is a fixed size buffer. In Linux, the size of the buffer is 1, i.e. 4K bytes, so that it is not the size of the file so as to grow without inspection. Using a single fixed buffer can also cause problems, such as may become full pipeline at the time of writing, when this happens, then write to the pipe () call will by default be blocked, waiting for some data to be read, so free up enough space for write () call to write.
2. Reading the process may work faster than the writing process. When all the current process data has been read, the pipeline is empty. When this happens, a subsequent read () call will by default be blocked, waiting for some data to be written, this solves the read () call to return to the issue of the end of the file.

Note: Read the data from the pipe is a one-time operation, once the data is read, it is discarded from the pipeline, in order to free up space to write more data.

Pipeline construction

    In Linux, the realization of the pipeline does not use specialized data structures, but by means of the inode inode file structure VFS and file system. By two file structure point to the same temporary VFS inode, inode VFS and this in turn points to a physical page achieved.

Read and write pipeline

     Pipe implementation source code fs / pipe.c, there are many functions in pipe.c, wherein there are two important functions, i.e., a read function pipeline pipe_read () function and the write pipe pipe_wrtie (). Write function pipeline by copying data written to the physical memory bytes VFS inode pointed to, and the pipe is read function by copying physical memory bytes read data. Of course, the kernel must use some mechanism to synchronize access to the pipe, for this purpose, kernel lock, and signal queue.
    When the writing process to write to the pipeline, which uses the standard library function write (), system file descriptors according to library functions delivered, can be found in the file structure of the file. configuration file specifies the function to perform the write operation (i.e., the write function) is, then, the kernel calls the function to complete the write operation. Writing function before writing data to memory, you must first check the information in the inode VFS, meeting the following requirements, in order to replicate the actual working memory:
1. there is enough memory space to be written to accommodate all data;
2. read program memory is not locked.
If the above conditions are satisfied, lock the memory write function first, then copy the data from the write process's address space into memory. Otherwise, the write process will sleep in the waiting queue VFS index nodes, then the kernel will call the scheduler, and the scheduler will select another process to run. When the actual write process can be interrupted in the waiting state, when the memory has enough space to accommodate the written data, or memory is unlocked, the process wakes read write process, this time, the write process the received signal. When data is written to memory, the memory is unlocked, and all will be awakened dormant in the process of reading the inode.
Reading process and writing process is similar to the pipe. However, the process can be returned when no data is locked or memory error message immediately, instead of blocking the process, depending on the file or pipe open mode. Conversely, the process may sleep waiting for the write process to write data in the queue the index node. After all the process is completed the pipeline operation, the pipe inode is discarded, and the shared data page also released.
     Because the realization of the pipeline operations involving many files, so when readers learn about the complete contents of the file system for later reading of the code pipe.c, you will feel not difficult to understand.
    Linux pipeline is limited to a write operation before blocking size. Specifically for each kernel buffer exact channels used is 4096 bytes. Unless the reader to empty the pipeline, or more than once a 4K write operation will be blocked. In fact, this is not really any limitations, because read and write operations are implemented in different threads.

References:
    What is linux pipeline

Published 94 original articles · won praise 237 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_38646470/article/details/79564392