EmbLogic's Blog

about fifo

Definition:
A FIFO is similar to a pipe. A FIFO (First In First Out) is a one-way flow of data. FIFOs have a name, so
unrelated processes can share the FIFO. FIFO is a named pipe. This is the main difference between pipes
and FIFOs.
Creat: A FIFO is created by the mkfifo function:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
pathname – a UNIX pathname (path and filename). The name of the FIFO
mode – the file permission bits.
FIFO can also be created by the mknod system call,
e.g., mknod(“fifo1”, S_IFIFO|0666, 0) is same as mkfifo(“fifo1”, 0666).
Open: mkfifo tries to create a new FIFO. If the FIFO already exists, then an EEXIST error is returned. To
open an existing FIFO, use open(), fopen() or freopen()
Close: to close an open FIFO, use close(). To delete a created FIFO, use unlink().
Properties: ( Some of them are also applicable to PIPES)
1) After a FIFO is created, it can be opened for read or write.
2) Normally, opening a FIFO for read or write, it blocks until another process opens it for write or read.
3) A read gets as much data as it requests or as much data as the FIFO has, whichever is less.
4) 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 4k1.
1
Why?
(1)The main operations (system calls) on PIPE or FIFO are write(…,length) and read(…,length).
(2)The length of write() or read() should not exceed 4k if we want the code portable among different unix systems.
(3) That is, all systems should guarantee write(…,length) or read(…,length) correctly executable when length <= 4k.
(4) Accordingly, the capacity of PIPE/FIFO should be at least 4k.
(5) Statements 1 and 2 are excerpted from textbooks.
5) One step further :How to verify the minimum capacity of a FIFO ? I have verified that the
capacity of a PIPE or a FIFO on my Unix System is 9k.
6) Blocked if read from an empty FIFO, or write to a full FIFO.
7) The O_NDELAY flag or O_NONBLOCK flag can be set for FIFO to affect the behavior of various
operations. This prevents accesses to the FIFO from blocking. See the Table below.
Example: how to set the flags?
writefd=open(FIFO1, O_WRONLY | O_NONBLOCK, 0);

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>