Named Pipe or FIFO with example C program

Named pipes, also referred to as FIFOs (First In, First Out), are essential IPC (Interprocess Communication) mechanisms in Unix-like systems. They provide a method for communication between unrelated processes running on the same system. Unlike anonymous pipes, named pipes exist as special files in the filesystem and can be accessed by any process with appropriate permissions.

Named pipes follow the FIFO principle, ensuring that data written by one process is read by another process in the same order. This makes them particularly useful for producer-consumer scenarios and process synchronization.

Syntax

int mkfifo(const char *pathname, mode_t mode);
int open(const char *pathname, int flags);
ssize_t write(int fd, const void *buf, size_t count);
ssize_t read(int fd, void *buf, size_t count);
Note: Named pipe programs use system calls that require a Unix-like environment. The example below demonstrates the concept but may not execute in all online compilers due to system-level dependencies.

Example: Named Pipe Communication

The following example demonstrates interprocess communication using named pipes. We create a FIFO, fork a child process where the child writes data and the parent reads it

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>

#define FIFO_PATH "/tmp/myfifo"

void writer_process() {
    int fd;
    char message[] = "Hello from named pipe!";
    
    /* Open named pipe for writing */
    fd = open(FIFO_PATH, O_WRONLY);
    if (fd == -1) {
        perror("Writer: open failed");
        exit(EXIT_FAILURE);
    }
    
    /* Write message to pipe */
    if (write(fd, message, strlen(message) + 1) == -1) {
        perror("Writer: write failed");
        close(fd);
        exit(EXIT_FAILURE);
    }
    
    printf("Writer: Message sent: %s<br>", message);
    close(fd);
}

void reader_process() {
    int fd;
    char buffer[100];
    
    /* Open named pipe for reading */
    fd = open(FIFO_PATH, O_RDONLY);
    if (fd == -1) {
        perror("Reader: open failed");
        exit(EXIT_FAILURE);
    }
    
    /* Read message from pipe */
    if (read(fd, buffer, sizeof(buffer)) == -1) {
        perror("Reader: read failed");
        close(fd);
        exit(EXIT_FAILURE);
    }
    
    printf("Reader: Message received: %s<br>", buffer);
    close(fd);
}

int main() {
    pid_t pid;
    
    /* Create named pipe */
    if (mkfifo(FIFO_PATH, 0666) == -1) {
        perror("mkfifo failed");
        exit(EXIT_FAILURE);
    }
    
    /* Fork child process */
    pid = fork();
    
    if (pid == -1) {
        perror("fork failed");
        unlink(FIFO_PATH);
        exit(EXIT_FAILURE);
    } else if (pid == 0) {
        /* Child process acts as writer */
        writer_process();
    } else {
        /* Parent process acts as reader */
        reader_process();
        
        /* Wait for child to complete */
        wait(NULL);
        
        /* Remove named pipe */
        unlink(FIFO_PATH);
    }
    
    return 0;
}

Expected Output

Writer: Message sent: Hello from named pipe!
Reader: Message received: Hello from named pipe!

Key Features

  • Persistent: Named pipes exist in the filesystem until explicitly removed
  • Bidirectional: Can be opened for reading, writing, or both
  • Blocking I/O: Read/write operations block until data is available or space is free
  • Process Independence: Communicating processes need not have parent-child relationship

Use Cases

  • Client-Server Communication: Server creates FIFO, clients connect to send requests
  • Log Processing: One process writes logs, another reads and processes them
  • Shell Scripting: Connecting command pipelines across different shell sessions
  • Producer-Consumer Systems: Data streaming between independent processes

Advantages and Limitations

Advantages Limitations
Simple programming interface No built-in synchronization
Process independence Limited error handling
Filesystem-based addressing Platform-specific (Unix-like systems)
Efficient buffered I/O Blocking behavior by default

Conclusion

Named pipes provide a robust mechanism for interprocess communication in Unix-like systems. They offer simplicity and efficiency for scenarios where processes need to exchange data without complex synchronization requirements. While they have limitations, FIFOs remain a fundamental tool in system programming and process coordination.

Updated on: 2026-03-15T14:31:20+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements