A named pipe, also known as a FIFO (first-in, first-out) in Linux, is a type of inter-process communication (IPC) mechanism that allows processes to communicate with each other by exchanging data through a shared memory buffer. Named pipes are an extension to the traditional pipe concept on Linux. (Please click here to read more about pipe concept). Named pipes are similar to ordinary pipes, which are used to transmit data between processes that are related to each other but named pipes can be accessed using a name rather than a file descriptor.

Named pipes are useful in situations where it is necessary to transfer data between processes in a reliable, synchronous manner. They can be used to pass data between processes running on the same machine, or between processes on different machines connected via a network. Named pipes are also useful for communicating with programs that do not support standard input/output streams or sockets.

There are few key differences between named-pipe and pipe such as

  1. Named pipes are accessed using a name rather than a file descriptor. This allows processes that are not related to each other to communicate using a common named pipe.
  2. Named pipes can be opened in both reading and writing modes by multiple processes. This allows processes to communicate in both directions.
  3. Named pipes are persistent, meaning that they remain in the file system even after all processes using the pipe have finished. This allows processes to communicate with each other at different times.

To create a named pipe in Linux, use the mkfifo command followed by the name you want to give to the pipe. For example, to create a named pipe called myNamedPipe, you would use the following command:

mkfifo myNamedPipe

In C language we can create the named pipe in following manner

// FIFO file path
char * myNamePipe = "/tmp/myfifo";

// Create named pipe. As we need to read & write so setting permission to 666
if(mkfifo(myNamePipe , 666) != 0){
    printf("Error in creating named pipe\n");
}

Named pipe can be accessed like regular files, using file input/output functions such as ‘open‘, ‘read‘, and ‘write‘ functions of C. However, unlike regular files, named pipes do not have a backing store on the file system and exist only in memory.

Unlike regular files, named pipes do not have a backing store on the file system and exist only in memory.

To access the named pipe, we can use the cat or less commands to read from it, or the echo command to write to the pipe. For example, to write data to the named pipe myNamedPipe, you can use the following command:

echo "Hello, world!" > myNamedPipe

To read data from the named pipe myNamedPipe, you can use the following command:

cat myNamedPipe

Summary: Named pipes are useful for passing data between processes that may not be running at the same time or that may not have a direct means of communication. For example, a process running in the background could write data to a named pipe, and a process running in the foreground could read the data from the pipe and display it to the user.

Named pipes are also useful for creating a simple form of inter-process communication in shell scripts, allowing different parts of a script to communicate with each other using the same file descriptor.

To read more about named pipe please visit Linux man page.

One thought on “How does a named Pipe (FIFO) work in Linux?”

Comments are closed.