Named Pipe (FIFO)

FIFO is a #Pipe that have names and directory link. This extends its availability to other processes that are not necessarily related to each other to establish an #Interprocess Communication (IPC) channel between them. Although the blocking on read and write when the queue is empty or full is the default, we can specify it to be non-blocking (flag O_NONBLOCK or O_NDELAY) which doesn’t wait for data or processes. Non-blocking read and write in FIFO also allow Concurrent writing. Atomic write is guaranteed up to 4096 bytes.

Creating FIFO can be done by calling the #Unix System Call mkfifo() (exported with the sys/types.h and sys/stat.h library) with the name of the FIFO file and permission of the FIFO file passed in as parameters. open() must be used on the FIFO file in order to have access to the pipe. An example of FIFO creation is shown below:

if (mkfifo("fifofile", 0644) < 0) {
  perror("fail to create FIFO");
  exit(1);
}

fd = open("fifofile", O_WRONLY | O_NONBLOCK)

There could be a case where after the client reading the FIFO has terminated, the server still need to wait for another request from the same or other clients. However, because read() will return zero once there is no process reading the FIFO file, the server has to open() the FIFO file again. The solution is to keep the FIFO waiting for input by having one process always open the write end of the FIFO. The server should open the FIFO in both read and write (although the write end will not be used in this sense) mode separately, meaning to open() FIFO two times with read only and write only mode respectively.

#operating-system