A FIFO is similar to a pipe. A FIFO is a one-way flow of data (First In First Out). FIFOs have a name, so unrelated processes can share the FIFO. FIFO is a named pipe.
FIFO和PIPE基本差不多,但FIFO是命名的,一些没有亲缘关系的process能共享它。
Normally, opening a FIFO for read or write, it blocks until another process opens it for write or read. Write and read必须一一对应。
A read gets as much data as it requests or as much data as the FIFO has, whichever is less.
A write to a FIFO is atomic, as long as the write does not exceed the capacity of the FIFO. The capacity is at least 4k.
How to set flags.
writefd = open (FIFO1, O_WRONLY|O_ONOBLOCK,0);
但是pipe没有open函数
所以只能这样设定
flags= fcntl (fd, F_GETFL,0);
flag|=O_NONBLOCK;
fcntl =(fd,F_SETFL,flags);
下面的表很重要,要看清下面的前提操作和当前操作,主要比较了Blocking和O_NONBLOCK条件下的区别
Operation | Existing opens of pipe or FIFO | Blocking (default) | O_NONBLOCK set |
Open FIFO for reading | FIFO open for writing | Returns OK | Returns OK |
FIFO not open for writing | Blocks until FIFO is opened for writing | Returns OK |
Open FIFO for writing | FIFO open for reading | Returns OK | Returns OK |
FIFO not open for reading | Blocks until FIFO is opened for reading | Returns an error of ENXIO |
Read empty pipe or FIFO | Pipe or FIFO open for writing | Blocked until there is data or the pipe or FIFO is closed for writing | Return an error of EAGAIN |
Pipe or FIFO not open for writing | Read returns 0 (EOF) | Read return 0 (EOF) |
Write to pipe or FIFO | Pipe or FIFO open for reading | Return ok | Return ok |
Pipe or FIFO is full | Blocked until space is available, then write data | Returns an error of EAGAIN |
Pipe or FIFO not open for reading | SIGPIPE generated, write process terminated | Returns an error of EPIPE |