Unix - Named Pipes (FIFOs) in UNIX: Enabling Communication Between Processes

Named Pipes, also known as FIFOs (First In, First Out), are a special type of file in UNIX that allows two or more processes to communicate with each other. Unlike ordinary files, named pipes do not store data permanently on the disk. Instead, they provide a temporary communication channel where one process writes data, and another process reads it. The name "FIFO" reflects the order in which data is handled—the first piece of data written into the pipe is the first piece to be read.

Named pipes differ from anonymous pipes in an important way. Anonymous pipes are created temporarily and usually exist only between related processes, such as a parent process and its child process. They disappear once the processes terminate. Named pipes, however, are created as special files in the file system and can be accessed by unrelated processes, provided they have the necessary permissions. This makes named pipes more flexible for inter-process communication (IPC) in UNIX systems.

How Named Pipes Work

A named pipe acts like a shared communication channel. One process opens the pipe for writing, while another process opens it for reading. Data written by the writer immediately becomes available to the reader. Unlike regular files, the data is not stored permanently. Once it has been read, it is removed from the pipe, making room for new data.

The communication follows these basic steps:

  1. A named pipe is created in the file system.

  2. A writing process opens the pipe and sends data.

  3. A reading process opens the same pipe and retrieves the data.

  4. After the data is read, it is no longer available in the pipe.

  5. The named pipe itself remains in the file system until it is explicitly deleted.

Creating a Named Pipe

The mkfifo command is used to create a named pipe.

Example:

mkfifo mypipe

This command creates a special FIFO file named mypipe.

To verify its creation, use:

ls -l mypipe

Example output:

prw-r--r-- 1 user user 0 Jul 4 10:30 mypipe

The first character p indicates that the file is a named pipe rather than a regular file.

Writing Data to a Named Pipe

One terminal can be used to write data into the pipe.

Example:

echo "Welcome to UNIX" > mypipe

This command waits until another process opens the pipe for reading.

Reading Data from a Named Pipe

In another terminal, execute:

cat < mypipe

Output:

Welcome to UNIX

Once the message has been read, it disappears from the pipe because FIFOs do not permanently store data.

Continuous Communication Through Named Pipes

Named pipes can also support continuous communication between processes.

Reader terminal:

cat < mypipe

Writer terminal:

echo "Message 1" > mypipe
echo "Message 2" > mypipe
echo "Message 3" > mypipe

Each message is delivered to the reader in the exact order it was written.

Blocking Behavior

One characteristic of named pipes is their blocking behavior.

  • If a process tries to read from a pipe before any process is writing to it, the reader waits.

  • If a process writes to a pipe before any reader is available, the writer also waits.

This synchronization ensures that data is transferred only when both the reading and writing processes are ready.

Permissions on Named Pipes

Like regular files, named pipes have ownership and permissions.

Permissions can be changed using:

chmod 660 mypipe

Ownership can be modified using:

chown username mypipe

These permissions determine which users can read from or write to the named pipe.

Practical Applications of Named Pipes

Named pipes are useful in many UNIX applications.

  • Allow communication between independent programs.

  • Transfer data from one process to another without creating temporary files.

  • Connect shell scripts with background services.

  • Exchange information between monitoring tools and logging utilities.

  • Facilitate communication in client-server applications running on the same system.

Advantages of Named Pipes

Named pipes provide several benefits.

  • Easy to create and use.

  • Enable communication between unrelated processes.

  • Require very little system memory.

  • Eliminate the need for temporary files.

  • Preserve the order of transmitted data.

  • Supported by almost all UNIX and UNIX-like operating systems.

Limitations of Named Pipes

Despite their usefulness, named pipes have some limitations.

  • Communication is limited to processes on the same machine.

  • They support sequential data transfer only.

  • They are not suitable for complex communication involving multiple simultaneous readers and writers.

  • Reading and writing operations may block if the corresponding process is not available.

  • They do not provide permanent storage for data.

Named Pipes vs. Anonymous Pipes

Feature Named Pipes (FIFO) Anonymous Pipes
File system entry Yes No
Communication Unrelated processes Parent and child processes
Lifetime Until deleted Until processes terminate
File name Yes No
Accessibility Multiple independent processes Related processes only

Best Practices

When working with named pipes, it is recommended to:

  • Remove unused FIFOs after they are no longer needed.

  • Set appropriate file permissions to prevent unauthorized access.

  • Handle blocking conditions in scripts and applications.

  • Use named pipes for lightweight local communication rather than large-scale data transfer.

  • Consider sockets or message queues when more advanced communication features are required.

Conclusion

Named pipes (FIFOs) are a simple yet powerful inter-process communication mechanism in UNIX. They allow unrelated processes to exchange data through a special file in the file system while maintaining the order of transmission. Because they are lightweight, easy to create, and require minimal resources, named pipes are widely used in shell scripting, system administration, automation, and local client-server communication. Although they are limited to communication on the same system and offer only sequential data transfer, they remain an efficient solution for many UNIX-based applications.