Linux: Named Pipes(FIFO)

A named pipe is really just a special kind of file (a FIFO file) on the local hard drive. Unlike a regular file, a FIFO file does not contain any user information. Instead, it allows two or more processes to communicate with each other by reading/writing to/from this file.

A named pipe works much like a regular pipe, but does have some noticeable differences.
  • Named pipes exist as a device special file in the file system.
  • Processes of different ancestry can share data through a named pipe.
  • When all I/O is done by sharing processes, the named pipe remains in the file system for later use.
The easiest way to create a FIFO file is to use the mkfifo command. This command is part of the standard Linux utilities and can simply be typed at the command prompt of your shell. You may also use the mknod command to accomplish the same thing.

prompt> mkfifo /tmp/myFIFO

we can also use of the mknod() system call:

LIBRARY FUNCTION: mknod(); 
PROTOTYPE: int mknod( char *pathname, mode_t mode, dev_t dev);

RETURNS: 0 on success, -1 on error: 
              errno = EFAULT (pathname invalid) 
                         EACCES (permission denied) 
                         ENAMETOOLONG (pathname too long) 
                         ENOENT (invalid pathname) 
                         ENOTDIR (invalid pathname)

mknod("/tmp/MYFIFO", S_IFIFO|0666, 0);

Normally, blocking occurs on a FIFO. In other words, if the FIFO is opened for reading, the process will "block" until some other process opens it for writing. This action works vice-versa as well. If this behavior is undesirable, the O_NONBLOCK flag can be used in an open() call to disable the default blocking action

No comments:

Post a Comment