Between development diary -20190823 linux named pipe fifo, interprocess communication

Original link: https://www.cnblogs.com/qingergege/p/9489682.html

And reproduced in gratitude: between linux named pipe fifo, interprocess communication

Named pipe (FIFO) is different from the unnamed pipes in that it provides a path name associated with it, in the form of a FIFO file exists in the file system so that, even if the process of creating genetic relationship with the FIFO process does not exist, as long as you can the access path, it is possible to communicate with one another through the FIFO, and therefore, through the FIFO unrelated processes can exchange data.

Named pipe (FIFO) and unnamed pipes (pipe) have some of the characteristics are the same, not the same place that:

1, FIFO exists in the file system as a special file, but the content was stored in the FIFO memory.

2, when the process exits to use the FIFO, FIFO will continue to save the file in the file system for later use.

3, FIFO has a name, unrelated processes can communicate by opening the named pipe.

int mkfifo (const char * pathname, mode_t mode); for creating a pipeline

int open (const char * pathname, int flags); a conduit for opening

Open FIFO files and regular files have a 2-point difference:

The first file is not open in FIFO mode O_RDWR read and write operations. Do such behavior is undefined.

Because we use FIFO usually only for one-way transmission of data, so there is no need to use this mode.

If you do need two-way transfer of data between programs, or preferably a pair of FIFO pipeline, using a one direction. In the method employed to close or reopen the FIFO clear to change the direction of data flow.

The second is the use of the flag O_NONBLOCK option.

Use this option only changes how the open call, but also to change the handling of file descriptor returned by the open call to read and write requests.

O_RDONLY, O_WRONLY and O_NONBLOCK flags There are four legitimate combinations:

flags = O_RDONLY: open call will be blocked, unless there is another way to open the process to write the same FIFO, otherwise it has been waiting for.
flags = O_WRONLY: open call will be blocked, unless there is another way to open the process to read the same FIFO, otherwise it has been waiting for.
flags = O_RDONLY | O_NONBLOCK: If at this time there is no other way to open the process to write FIFO, this time will open the successful return, this time to open the FIFO is read, and does not return an error.
flags = O_WRONLY | O_NONBLOCK: return immediately, if at this time there is no other way to open the process to read, open fail to open, then FIFO is not open, it returns -1.
open the function call parameter flag O_NONBLOCK affects the FIFO read and write operations.

Rules are as follows:

Read the call to the blocking of an empty FIFO will wait until the data can be read when it continues to execute /
read call to an empty non-blocking FIFO immediately return 0 bytes.
For a complete obstruction of the FIFO write call will wait until the data can be written to only begin.
System requirements: If the data length is less than or equal PIPE_BUF written bytes, then all bytes or write, or not write a byte.
Note that this restriction effect:
when only a FIF and allow a plurality of different programs to process the transmission request FIFO read time, in order to ensure data blocks from different programs are not mutually staggered, i.e. each atomic operations, this restriction very important. If all the buns can be sent to the write request is a blocking of the FIFO, each write request and data length parent PIPE_BUF bytes or less, the system can ensure that the data will not be intertwined. FIFO usually will pass through each data length is limited to PIPE_BUF it is a good idea.

In the case of non-blocking write call, if the FIFO is not receiving all the data written in accordance with the following rules:
length of the data requested to be written is smaller than PIPE_BUF bytes, the call fails, data can not be written.
Length of the data requested to be written is larger than PIPE_BUF bytes, the data write section, returns the number of bytes actually written, the return value may be zero.
among them. PIPE_BUF is the length of the FIFO, which is defined in the header file in limits.h. In linux or other UNIX-like systems, its value is usually 4096 bytes.

Guess you like

Origin blog.csdn.net/qq_31433709/article/details/100026292