Course contentsShow
← Linux
Lesson 291 of 1,8193. The Filesystem Hierarchy and POSIX StandardsFree lesson

Interprocess Communication via Named Pipes

Utilize FIFOs as a mechanism for stream-oriented communication between independent processes.

Interprocess Communication via Named Pipes

In this lesson, you will learn how to use First-In, First-Out (FIFO) files to enable two independent processes to exchange data streams directly through the filesystem.

The Concept of a Named Pipe

In our previous exploration of the Virtual Filesystem (VFS) and file descriptors, you learned that processes interact with data via files. A Named Pipe (or FIFO) is a special type of file that acts as a conduit. Unlike a regular file that stores bytes on a disk platter or SSD, a FIFO acts as a memory buffer that connects the output of one process to the input of another.

When Process A writes to a FIFO, the data isn't saved to the disk; it is held in a kernel buffer until Process B reads it. This follows the POSIX standard for interprocess communication, allowing unrelated processes—which don't share a parent-child relationship—to "talk" to each other simply by reading from and writing to the same filename in the directory hierarchy.

Practical Implementation

You can create a FIFO using the mkfifo command. Once created, it appears as a file with a p permission flag (check this with ls -l).

Try this in two separate terminal windows:

  1. Terminal 1: Create the pipe and wait for data:

    mkfifo my_pipe
    cat < my_pipe
    
  2. Terminal 2: Send data into the pipe:

    echo "Hello from the other side!" > my_pipe
    

In this example, cat blocks until the echo command writes into the pipe. The kernel handles the synchronization automatically. As soon as the data is sent, cat receives it and prints it to your screen. Because the pipe resides within the filesystem hierarchy, any process with appropriate permissions can access it, provided they know the file path. This makes FIFOs a powerful, lightweight tool for stream-oriented communication between independent services.

Key Takeaway: Named pipes (FIFOs) provide a filesystem-based interface that allows independent processes to communicate via synchronous, stream-oriented data transfer without needing to write temporary files to the disk.