FIFOs (Named Pipes)
A FIFO (First-In First-Out), commonly called a Named Pipe, is a special file that provides byte-stream communication between processes. Unlike anonymous pipes, FIFOs exist in the file system and allow communication between unrelated processes.
Learning Objectives
- Understand FIFOs.
- Differentiate FIFOs from anonymous pipes.
- Learn the
mkfifo()system call. - Understand FIFO permissions.
- Study FIFO architecture.
- Prepare for FIFO programming examples.
1. What is a FIFO?
A FIFO is a special file maintained by the kernel that provides
a unidirectional byte-stream communication channel.
Processes communicate by opening the FIFO file and performing
standard file operations such as read() and
write().
Although a FIFO appears as a file in the file system,
the data itself is not stored on disk. The kernel transfers
the data through an internal buffer.
2. Why are FIFOs Needed?
| Requirement | Benefit |
|---|---|
| Communication between unrelated processes | No common parent required |
| Simple IPC | Uses normal file operations |
| Persistent communication endpoint | FIFO remains in the file system |
| Shell scripting | Easy integration with commands |
| Local IPC | Efficient kernel-managed buffering |
3. Pipe vs FIFO
| Feature | Anonymous Pipe | FIFO |
|---|---|---|
| Visible in File System | No | Yes |
| Related Processes | Usually Required | Not Required |
| Created By | pipe() |
mkfifo() |
| Pathname | No | Yes |
| Communication Style | Byte Stream | Byte Stream |
| Kernel Buffer | Yes | Yes |
4. FIFO Architecture
The FIFO pathname provides a rendezvous point for processes. Once opened, data flows through the kernel buffer rather than being stored in the file.
5. mkfifo()
The mkfifo() system call creates a FIFO special file.
| Return Value | Description |
|---|---|
| 0 | FIFO created successfully. |
| -1 | Error occurred. |
6. FIFO Permissions
Like regular files, FIFOs use UNIX file permissions to control access.
| Permission | Meaning |
|---|---|
| Read | Allows reading from the FIFO. |
| Write | Allows writing to the FIFO. |
| Execute | No special meaning for FIFOs. |
7. FIFO in the File System
A FIFO appears as a special file in the directory where it is created. The file stores metadata such as ownership and permissions, while the actual communication data remains in kernel memory.
8. Creating FIFOs Using the Shell
The mkfifo command creates a named pipe directly
from the shell.
9. Advantages of FIFOs
- Communication between unrelated processes.
- Simple programming model.
- Standard file operations.
- Persistent communication endpoint.
- Well supported by UNIX systems.
10. Interview Questions
- What is a FIFO?
- How does a FIFO differ from an anonymous pipe?
- Which system call creates a FIFO?
- Can unrelated processes communicate using a FIFO?
- Where is a FIFO visible?
- Does a FIFO store data on disk?
- Which header file declares
mkfifo()? - Why are FIFOs called named pipes?
- How are permissions applied to a FIFO?
- What shell command creates a FIFO?
11. Opening a FIFO
Unlike anonymous pipes, a FIFO is opened using the standard
open() system call.
Typical flags include:
| Flag | Description |
|---|---|
O_RDONLY |
Open for reading. |
O_WRONLY |
Open for writing. |
O_RDWR |
Open for both reading and writing. |
O_NONBLOCK |
Enable non-blocking mode. |
12. FIFO Open Behavior
| Operation | Behavior |
|---|---|
| Open for Reading | Blocks until a writer opens the FIFO (default behavior). |
| Open for Writing | Blocks until a reader opens the FIFO. |
Open with O_NONBLOCK |
Returns immediately according to POSIX rules. |
13. Reading from a FIFO
After opening the FIFO for reading, the process uses the
standard read() system call.
Bytes are read in FIFO order.
14. Writing to a FIFO
A writer transfers data using the standard
write() system call.
The kernel delivers the data to any process reading from the FIFO.
15. End-of-File (EOF)
When every writer has closed the FIFO and all buffered data has
been consumed, a subsequent read() returns
0, indicating End-of-File.
16. FIFO File Descriptors
Once opened, a FIFO is referenced through an ordinary file descriptor, just like a regular file.
| Operation | System Call |
|---|---|
| Open | open() |
| Read | read() |
| Write | write() |
| Close | close() |
17. Example: Reader Process
18. Example: Writer Process
19. Linux Commands
| Command | Description |
|---|---|
mkfifo /tmp/myfifo |
Create a FIFO. |
ls -l /tmp/myfifo |
Display FIFO information. |
file /tmp/myfifo |
Identify the file as a FIFO. |
rm /tmp/myfifo |
Remove the FIFO. |
20. Interview Questions
- Which system call opens a FIFO?
- Why is
open()used instead ofpipe()? - What happens when a reader opens a FIFO before a writer?
- When does
read()return EOF? - Can multiple processes open the same FIFO?
- Why is a FIFO treated like a normal file?
- What happens if
O_NONBLOCKis used? - Can unrelated processes communicate through a FIFO?
- Why does the FIFO pathname remain after communication ends?
- Which standard I/O system calls operate on FIFOs?
21. Blocking vs Non-blocking FIFOs
By default, FIFO operations use blocking I/O. A process may wait until another process opens the opposite end of the FIFO or until data becomes available. Non-blocking mode changes this behavior by returning immediately instead of waiting.
| Mode | Behavior |
|---|---|
| Blocking | Process waits until the operation can proceed. |
| Non-blocking | System call returns immediately if it cannot proceed. |
22. Using O_NONBLOCK
Non-blocking mode is enabled when opening the FIFO.
Applications using event loops or multiplexed I/O commonly enable non-blocking mode to avoid waiting indefinitely.
23. Multiple Readers and Writers
A FIFO may be opened simultaneously by multiple reader and writer processes. The kernel manages synchronization of the byte stream. However, applications must define their own message boundaries if multiple writers share the same FIFO.
24. SIGPIPE
If a process writes to a FIFO after all reader file descriptors have
been closed, the kernel generates the
SIGPIPE signal.
If the signal is ignored or handled, the
write() system call fails and returns
-1 with errno set to
EPIPE.
25. FIFO Kernel Buffer
Like anonymous pipes, FIFOs use a kernel-managed memory buffer for data transfer. The FIFO special file stores only metadata such as permissions and ownership. Communication data remains entirely within kernel memory.
26. Complete Communication Flow
The FIFO pathname remains in the file system until explicitly removed, even after all communicating processes terminate.
27. Useful Linux Commands
| Command | Description |
|---|---|
stat /tmp/myfifo |
Display detailed FIFO metadata. |
find /tmp -type p |
Locate FIFO special files. |
lsof /tmp/myfifo |
Show processes currently using the FIFO. |
man 7 fifo |
Display FIFO semantics. |
28. Best Practices
- Create the FIFO before starting communicating processes.
- Check all return values from
mkfifo(),open(),read(), andwrite(). - Remove unused FIFOs after communication completes.
- Handle
SIGPIPEappropriately. - Use non-blocking mode only when required by the application design.
- Design application-level message formats when multiple writers share a FIFO.
29. Advanced Interview Questions
- What is the difference between blocking and non-blocking FIFO operations?
- How is
O_NONBLOCKenabled? - Can multiple processes write to the same FIFO simultaneously?
- Why must applications define message boundaries when multiple writers exist?
- What causes the
SIGPIPEsignal for a FIFO? - Where is FIFO data actually stored?
- Why does the FIFO pathname remain after all processes exit?
- How can you determine which processes are using a FIFO?
- When would non-blocking FIFOs be preferred?
- How does FIFO communication differ from socket communication?
30. FIFO API Summary
| Function | Purpose |
|---|---|
mkfifo() |
Create a FIFO special file. |
open() |
Open the FIFO. |
read() |
Read bytes from the FIFO. |
write() |
Write bytes to the FIFO. |
close() |
Close the FIFO descriptor. |
unlink() |
Remove the FIFO pathname. |
31. FIFO Lifecycle
The FIFO pathname remains in the file system until it is explicitly
removed using unlink() or the
rm command.
32. Pipe vs FIFO vs Socket
| Feature | Pipe | FIFO | Socket |
|---|---|---|---|
| Related Processes | Usually Yes | No | No |
| Visible in File System | No | Yes | UNIX Domain Only |
| Communication Direction | Half-duplex | Half-duplex | Full-duplex |
| Uses Pathname | No | Yes | UNIX Domain Only |
| Network Communication | No | No | Yes |
| Kernel Buffer | Yes | Yes | Yes |
33. Common Programming Mistakes
- Creating a FIFO that already exists.
- Ignoring errors returned by
mkfifo(). - Forgetting to remove unused FIFOs.
- Assuming FIFO data is stored on disk.
- Ignoring blocking behavior.
- Not handling
SIGPIPE. - Using one FIFO when independent bidirectional communication is required.
34. Debugging Tips
| Problem | Possible Cause |
|---|---|
| open() blocks forever | Opposite endpoint not opened. |
| write() fails | No active readers. |
| FIFO already exists | Previous FIFO not removed. |
| No data received | Writer closed unexpectedly. |
| Application hangs | Blocking I/O waiting for peer. |
35. Best Practices
- Create the FIFO during application initialization.
- Choose appropriate file permissions.
- Remove temporary FIFOs when no longer needed.
- Validate every system call.
- Use application-level message formats for structured communication.
- Document FIFO locations for maintenance.
- Use sockets instead of FIFOs when communication must span multiple machines.
36. Advanced Interview Questions
- Explain the complete lifecycle of a FIFO.
- Why is a FIFO called a named pipe?
- What happens if a FIFO already exists when
mkfifo()is called? - When should a FIFO be removed?
- How is a FIFO different from a UNIX domain socket?
- Can multiple readers open the same FIFO?
- How does the kernel buffer FIFO data?
- What is the role of
unlink()? - When would sockets be preferred over FIFOs?
- What debugging tools are useful for FIFO communication?
37. Module Summary
In this module, we studied FIFOs (named pipes), which extend anonymous
pipes by providing a pathname in the file system. We learned how to
create FIFOs using mkfifo(), access them with
open(), communicate using read() and
write(), understand blocking behavior, kernel buffering,
non-blocking mode, SIGPIPE, debugging techniques, and
recommended programming practices.
FIFOs are suitable for local communication between unrelated
processes. The next module introduces the System V IPC facilities,
which provide message queues, semaphores, and shared memory for more
advanced inter-process communication.
Concept Mastery Assessment — Module 41
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.