Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are Named Pipes or FIFO in Linux/Unix systems?
Named Pipes (also called FIFO - First-In-First-Out) are a mechanism for inter-process communication (IPC) in Linux/Unix systems that allows unrelated processes to communicate with each other. Unlike anonymous pipes, which work only between parent and child processes, named pipes create a special file on the filesystem that can be accessed by any process with appropriate permissions.
Named pipes support bidirectional communication, meaning data can flow in both directions through a single pipe, making them more versatile than regular pipes for complex communication scenarios.
Creating Named Pipes
There are two primary ways to create named pipes in Linux/Unix systems:
Using mknod() System Call
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int mknod(const char *pathname, mode_t mode, dev_t dev);
The mknod() system call creates special files including FIFOs. The parameters are:
pathname − Path where the named pipe will be created
mode − File type and permissions
dev − Device information (unused for FIFOs)
Using mkfifo() Library Function
#include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode);
The mkfifo() function is specifically designed for creating FIFO files and is simpler to use than mknod().
File Modes and Permissions
File types for mknod():
| File Type | Description | File Type | Description |
|---|---|---|---|
| S_IFBLK | Block special | S_IFREG | Regular file |
| S_IFCHR | Character special | S_IFDIR | Directory |
| S_IFIFO | FIFO special | S_IFLNK | Symbolic link |
Permission modes:
| File Mode | Description | File Mode | Description |
|---|---|---|---|
| S_IRWXU | Read, write, execute by owner | S_IWGRP | Write permission, group |
| S_IRUSR | Read permission, owner | S_IXGRP | Execute permission, group |
| S_IWUSR | Write permission, owner | S_IRWXO | Read, write, execute by others |
| S_IXUSR | Execute permission, owner | S_IROTH | Read permission, others |
| S_IRWXG | Read, write, execute by group | S_IWOTH | Write permission, others |
| S_IRGRP | Read permission, group | S_IXOTH | Execute permission, others |
Permissions can also be specified in octal notation (0XYZ), where X = owner, Y = group, Z = others. Values: read=4, write=2, execute=1. For example, 0640 means read+write (6) for owner, read (4) for group, no permissions (0) for others.
One-Way Communication Example
Server Process Steps:
Creates named pipe "MYFIFO" using
mknod()Opens the pipe in read-only mode
Waits for messages from client
Prints received messages until "end" is received
Client Process Steps:
Opens the named pipe in write-only mode
Accepts user input and sends to server
Terminates when user enters "end"
Two-Way Communication
For bidirectional communication, both processes open the named pipe in read-write mode (O_RDWR). The server receives messages, processes them (e.g., reverses the string), and sends responses back through the same FIFO.
Advantages and Disadvantages
| Advantages | Disadvantages |
|---|---|
| Communication between unrelated processes | Limited to processes on same machine |
| Bidirectional communication possible | Slower than anonymous pipes |
| Persistent in filesystem | Requires filesystem cleanup |
| Simple to implement | No message boundaries |
Conclusion
Named Pipes (FIFOs) provide a powerful IPC mechanism for unrelated processes in Linux/Unix systems. They support bidirectional communication and persist in the filesystem, making them ideal for client-server applications running in separate terminals or processes. While slower than anonymous pipes, they offer greater flexibility for inter-process communication scenarios.
