Saturday 3 December 2016

FIFOs

      A first-in, first-out (FIFO) file is a pipe that has a name in the filesystem. Any process can open or close the FIFO; the processes on either end of the pipe need not be related to each other.
      FIFOs are also called named pipes.
      You can make a FIFO using the “mkfifo” command. Specify the path to the FIFO on the command line. For example, create a FIFO in /tmp/fifo by invoking this command:
      $ mkfifo /tmp/fifo
      $ ls -l /tmp/fifo
prw-rw-rw-    1 samuel   users           0 Jan 16 14:04 /tmp/fifo
     The first character of the output from ls is p, indicating that this file is actually a FIFO (named pipe).

Creating a FIFO
      Create a FIFO programmatically using the mkfifo function.
      Synopsis
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
     The first argument is the path at which to create the FIFO;
     the second parameter specifies the pipe’s owner, group, and world permissions,

Accessing a FIFO
      Access a FIFO just like an ordinary file.
      To communicate through a FIFO, one program must open it for writing, and another program must open it for reading.
      Either low-level I/O functions (open, write, read, close, and so on) or C library I/O functions (fopen, fprintf, fscanf, fclose, and so on) may be used.
      For example, to write a buffer of data to a FIFO using low-level I/O routines, you could use this code:
int fd = open (fifo_path, O_WRONLY);
write (fd, data, data_length);
close (fd);

      Bytes from each writer are written atomically up to a maximum size of PIPE_BUF (4KB on Linux)

No comments:

Post a Comment