Implementation of FIFO in Linux

Implementation of FIFO in Linux

INTRODUCTION

In Linux, inter-process communication (IPC) is vital for allowing different processes to exchange data and coordinate tasks. One of the simplest and most effective IPC mechanisms is the FIFO (First In, First Out), also known as a named pipe. Unlike regular pipes that can only be used between related processes, FIFOs allow communication between unrelated processes, making them highly flexible in complex applications.

A FIFO provides a file-based communication method where data written first is read first, ensuring orderly transmission. It can be created either through simple command-line tools like mkfifo and mknod, or programmatically in C using system calls. In this guide, we’ll explore how to create, open, read from, and write to FIFOs both manually and in C programming, offering a step-by-step approach to building efficient communication between processes in Linux.

Whether you’re a system programmer, a Linux enthusiast, or someone curious about IPC mechanisms, understanding FIFOs will deepen your grasp of Linux internals and enhance your programming skills.


 

Implementation of FIFO in  Linux

  1. It is a named pipe, a method for passing information from one computer process to other processes using a pipe or message holding place that is given a specific name. Unlike a regular pipe, a named pipe can be used by processes that do not have to share a common process origin and the message sent to the named pipe can be read by any authorized process that knows the name of the named pipe.
  2. A named pipe is sometimes called a “FIFO” (first in, first out) because the first data written to the pipe is the first data that is read from it.

Create FIFO using in Commands

  • mkfifo filename
  • mknod filename p

Accessing a FIFO

  1. First, try reading the (empty) FIFO: cat < my_fifo
  2. Now try writing to the FIFO. You will have to use a different terminal because the first command will now be hanging, waiting for some data to appear in the FIFO.
  3. echo “Hello World” > my_fifo
  • Create a FIFO in C

#include<sys/types.h>

#include<sys/stat.h>

 int mkfifo(const char *filename, mode_t mode);

 int mknod(const char *filename, mode_t mode | S_IFIFO, (dev_t) 0);

Like the mknod and mkfifo command, you can use the mknod function for making many special types of files. Using a dev_t value of 0 and ORing the file access mode with S_IFIFO is the only portable use of this function that creates a named pipe.

  • Opening a FIFO

Unlike unnamed Pipe, FIFO needs to be opened for reading and writing so the file descriptors can be used along with it.

fd = open(const char *path, O_RDONLY);

fd = open(const char *path, O_WRONLY);

  • The write system call

 #include

 size_t write(int fildes, const void *buf, size_t nbytes);

It arranges for the first nbytes bytes from buf to be written to the file associated with the file descriptor fildes.

It returns the number of bytes actually written. This may be less than nbytes if there has been an error in the file descriptor. If the function returns 0, it means no data was written; if it returns –1, there has been an error in the write call.

  • The read system call

#include

 size_t read(int fildes, void *buf, size_t nbytes);

It reads up to nbytes bytes of data from the file associated with the file descriptor fildes and places them in the data area buf.

It returns the number of data bytes actually read, which may be less than the number requested. If a read call returns 0, it had nothing to read; it reached the end of the file. Again, an error on the call will cause it to return –1.

Steps to Implement Pipe Between Unrelated Processes (Using FIFO):

  1. Create a named pipe (FIFO)using:
  • Command line: mkfifo filename
  • Or in code: mkfifo(“filename”, 0666);
    1. Open the FIFO for writingin one process (writer):
  • Use open(“filename”, O_WRONLY);
    1. Write datainto the FIFO using write().
    2. Open the FIFO for readingin another process (reader):
  • Use open(“filename”, O_RDONLY);
    1. Read datafrom the FIFO using read().
    2. Close the FIFOin both processes after communication.
    3. (Optional)Remove the FIFO using unlink(“filename”); when it’s no longer needed.