Famous pipelines in linux (FIFO)

Source: Internet
Author: User
A major limitation of an unnamed pipeline application is that it has no name, so it can only be used for kinship-related inter-process communication, in a famous pipeline (namedpipe or FIF

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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.