Open rules for famous Pipelines
A famous Pipeline has one more open operation than the pipeline: open.
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 ).
Note:
When the read pipeline is opened, you must wait for the write pipeline to open, and the write pipeline to open must wait for the read pipeline to open.
Deadlock will not be generated, because the "wait" ** here is not * Whether the open of the other side is * completed *
For example,
If o_nonblock is not specified for the open FIFO, the process first opens the FIFO for reading and blocks the data.
Then process B open FIFO for writing. Because process a is open for reading, the open operation of process B will
Return immediately, and then a's open operation returns.
For more information about how to verify an open rule, see appendix 2.
2.4 read/write rules for famous Pipelines
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.
- If a process writes to enable FIFO and there is no data in the current FIFO, the read operation with the blocking flag is always blocked. -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.
- For read operations with the blocking flag configured, there are two reasons for blocking: data in the current FIFO, but other processes are reading the data. In addition, there is 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 (at this time, the read operation returns 0 ).
- If no process write is enabled, the read operation with the blocking flag set will be blocked.
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.
- 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;