A major restriction of an unnamed pipeline application is that it has no name, so it can only be used for Kinship-related inter-process communication. After proposed by a famous Pipeline (named pipe or FIFO, this restriction is overcome. FIFO is different from pipelines in that it provides a path name associated with it and exists in the file system as a FIFO file. In this way, even if there is no kinship with the FIFO creation process, as long as the path can be accessed, it can communicate with each other through FIFO (the process that can access this path and the process that creates the FIFO). Therefore, data can be exchanged through non-FIFO processes. It is worth noting that the first in first out (FIFO) is strictly followed, and data is always returned from the beginning for the read of the pipeline and the first in first out (FIFO, write to them to add the data to the end. They do not support file location operations such as lseek.
The buffer of the MPs queue is limited (the MPs queue is in the memory and a page size is allocated to the buffer when the MPs queue is created)
The pipeline transmits a non-formatted byte stream, which requires that the reader and writer of the pipeline have to specify the data format in advance, for example, how many bytes are counted as a message (or command, or record) and so on.
FIFO is usually composed of multiple write processes and one read process.
FIFO open rules:
If a read-only FIFO is enabled for the current open operation, if a corresponding process has enabled this FIFO for writing, the current open operation will be successful; otherwise, it may be blocked until a corresponding process opens the FIFO for writing (the blocking flag is set for the current open operation); or, it will be returned successfully (the blocking flag is not set for the current open operation ).
If the current enable operation is to enable the FIFO for writing, if a corresponding process has opened the FIFO for reading, the current enable operation will be successful; otherwise, it may be blocked until a corresponding process opens the FIFO for reading (the blocking flag is set for the current open operation); or, an ENXIO error is returned (the blocking flag is not set for the current open operation ).
In a word, once a blocking flag is set, after mkfifo is called, The read and write operations on both ends of the pipeline must be opened separately. If either side is not opened, it will be blocked when open is called.
Read data from FIFO:
Convention: if a process blocks the access to the FIFO to read data from the FIFO, the read operation in the process is a read operation with a blocking flag. (I want to open a famous Pipeline to read data now !)
If a process writes to enable FIFO, and there is no data in the current FIFO (it can be understood that both ends of the pipeline have been established, but the write end has not started writing data !)
For read operations with the blocking mark set, it will be blocked all the time (that is, it is blocked and waiting for data. It does not consume CPU resources. This process synchronization method is very efficient for the CPU .)
-1 is returned if no blocking mark is set for the read operation. The current errno value is EAGAIN, prompting you to try again later.
Description of the read operation with the blocking flag set (see the above conventions)
There are two reasons for blocking.
Data exists in the FIFO, but other processes are reading the data.
No data in the FIFO. The reason for blocking is that there are new data writes in the FIFO, regardless of the size of the data written to the message or the amount of data requested by the read operation.
The read blocking mark only applies to the first read operation of the process. If there are multiple read operation sequences in the process, after the first read operation is awakened and the read operation is completed, other read operations to be executed will not be blocked. Even if there is no data in the FIFO when the read operation is executed, the read operation returns 0.
NOTE: If data exists in the FIFO, the read operation with the blocking flag is not blocked because the number of bytes in the FIFO is smaller than the number of bytes requested to read, the read operation returns the existing data volume in the FIFO.
Write Data to FIFO:
Convention: if a process blocks the opening of the FIFO to write data to the FIFO, the write operation in the process is a write operation with a blocking flag.
Write operations with blocking flag configured:
When the data volume to be written is not greater than PIPE_BUF, linux ensures the atomicity of writing. If the idle buffer of the pipeline is insufficient to accommodate the number of bytes to be written, the system goes to sleep until the buffer can accommodate the number of bytes to be written. (PIPE_BUF ==>>/usr/include/linux/limits. h)
When the data volume to be written is greater than PIPE_BUF, linux will no longer guarantee the atomicity of writing. As soon as the FIFO buffer has an idle area, the write process will attempt to write data to the pipeline, and the write operation will return after writing all the data written by the request.
For write operations without blocking flag set:
When the data volume to be written is greater than PIPE_BUF, linux will no longer guarantee the atomicity of writing. After the buffer is fully written into all the FIFO idle buffers, the write operation returns.
When the data volume to be written is not greater than PIPE_BUF, linux ensures the atomicity of writing. If the current FIFO idle buffer can accommodate the number of bytes written by the request, the result is returned successfully. If the current FIFO idle buffer cannot accommodate the number of bytes written by the request, the EAGAIN error is returned, remind me to write it later;
Briefly describe the logic of the blocking flag above.
A blocking flag is set.
If (buf_to_write <= PIPE_BUF) // when the data volume written is not greater than PIPE_BUF, thenif (buf_to_write> system_buf_left) // ensure the atomicity of writing, or write all the buf_to_write at one time, either one byte is not written! Thenblock; until (buf_to_write <= system_buf_left); goto write; elsewrite; fielsewrite; // In any case, it is a constant write, and fi is blocked only when the buffer is full.