Named Pipe or FIFO with example C program
Last Updated :
25 Apr, 2025
In computing, a named pipe, also known as a FIFO (First In, First Out), is a powerful mechanism for inter-process communication (IPC). Unlike unnamed pipes, which are temporary and exist only as long as the process that created them is running, named pipes provide a persistent communication channel between processes, surviving beyond the life of the process that created them. This makes named pipes an essential tool for developers who need processes to exchange data reliably and efficiently.
What is a Named Pipe (FIFO)?
A named pipe is an extension of the traditional pipe concept in Unix. While a traditional pipe is "unnamed" and exists only temporarily, a named pipe can persist as long as the system is up or until it is explicitly deleted. Named pipes appear as special files in the filesystem, and multiple processes can attach to them for reading and writing, facilitating inter-process communication.
A FIFO file allows two or more processes to communicate by reading from and writing to the same file. This file type is created using the 'mkfifo()' system call in C. Once created, any process can open the named pipe for reading or writing, similar to how it would handle an ordinary file. However, it is important to note that a named pipe must be opened simultaneously at both ends (for reading and writing) before any input or output operations can occur.
How to Create a Named Pipe (FIFO) in Unix
To create a FIFO special file, you use the 'mkfifo()' function in C. The function creates a named pipe with the specified pathname and permissions.
C
int mkfifo(const char *pathname, mode_t mode);
'mkfifo()' makes a FIFO special file with name pathname. Here 'mode' specifies the FIFO's permissions. It is modified by the process's umask in the usual way: the permissions of the created file are (mode & ~umask).
Using FIFO: As named pipe(FIFO) is a kind of file, we can use all the system calls associated with it i.e. open, read, write, close.
Example Programs to illustrate the named pipe: There are two programs that use the same FIFO. Program 1 writes first, then reads. The program 2 reads first, then writes. They both keep doing it until terminated.
1. Program 1(Writes first)
C
// C program to implement one side of FIFO
// This side writes first, then reads
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd;
// FIFO file path
char * myfifo = "/tmp/myfifo";
// Creating the named file(FIFO)
// mkfifo(<pathname>, <permission>)
mkfifo(myfifo, 0666);
char arr1[80], arr2[80];
while (1)
{
// Open FIFO for write only
fd = open(myfifo, O_WRONLY);
// Take an input arr2ing from user.
// 80 is maximum length
fgets(arr2, 80, stdin);
// Write the input arr2ing on FIFO
// and close it
write(fd, arr2, strlen(arr2)+1);
close(fd);
// Open FIFO for Read only
fd = open(myfifo, O_RDONLY);
// Read from FIFO
read(fd, arr1, sizeof(arr1));
// Print the read message
printf("User2: %s\n", arr1);
close(fd);
}
return 0;
}
2. Program 2(Reads First)
C
// C program to implement one side of FIFO
// This side reads first, then reads
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int fd1;
// FIFO file path
char * myfifo = "/tmp/myfifo";
// Creating the named file(FIFO)
// mkfifo(<pathname>,<permission>)
mkfifo(myfifo, 0666);
char str1[80], str2[80];
while (1)
{
// First open in read only and read
fd1 = open(myfifo,O_RDONLY);
read(fd1, str1, 80);
// Print the read string and close
printf("User1: %s\n", str1);
close(fd1);
// Now open in write mode and write
// string taken from user.
fd1 = open(myfifo,O_WRONLY);
fgets(str2, 80, stdin);
write(fd1, str2, strlen(str2)+1);
close(fd1);
}
return 0;
}
Output: Run the two programs simultaneously on two terminals.








Conclusion
The Named pipes (FIFOs) are a robust method for the inter-process communication allowing data to be passed between the processes using a named file. This mechanism is useful in the scenarios where processes need to exchange data without direct knowledge of the each other. The example demonstrates a simple producer-consumer model where one process writes data to a FIFO and another reads it showcasing the basic usage of the named pipes in C.
Similar Reads
Predefined Macros in C with Examples According to C89 Standard, C programming language has following predefined macros: __LINE__ Macro: __LINE__ macro contains the current line number of the program in the compilation. It gives the line number where it is called. It is used in generating log statements, error messages, throwing excepti
4 min read
C program for pipe in Linux Working and implementation of Pipe in Linux. Prerequisite : Pipe in Linux Approach : Pipe is highly used in Linux. Basically, pipe has 2 parts, one part is for writing and another is used for reading. So, an array of size 2 is taken. a[1] is used for writing and a[0] for reading.After reading from p
1 min read
readelf command in Linux with Examples When we compile source code, an object file is generated of the program and with the help of linker, this object files gets converted to a binary file which, only the machine can understand. This kind of file follows some structures one of which is ELF(Executable and Linkable Format). And to get the
4 min read
Formatted and Unformatted Input/Output functions in C with Examples In C language, the Input/Output (I/O) functions are part of the standard library, and these functions are used for interacting with the user or other systems, to perform operations such as reading input and printing output. These functions provide ways to read data from files and other input devices
7 min read
printf command in Linux with Examples The 'printf' command in Linux is a versatile tool used to display formatted text, numbers, or other data types directly in the terminal. Like the 'printf' function in programming languages like C, the Linux printf command allows users to format output with great precision, making it ideal for script
4 min read
Output of C programs | Set 40 (File handling) Prerequisite : File handling 1. What is the output of this program by manipulating the text file? C #include <stdio.h> int main() { if (remove("myfile.txt") != 0) perror("Error"); else puts("Success"); return 0; } Options: a) Error b) Success c) Runtime Error d) C
3 min read
gdb command in Linux with examples GDB, the acronym for GNU Debugger, is a powerful debugging tool used to analyze and debug programs written in languages like C, C++, Ada, and Fortran. It allows developers to inspect the behavior of their programs, step through code, set breakpoints, and examine variable values in real-time. GDB is
8 min read
C program to detect tokens in a C program As it is known that Lexical Analysis is the first phase of compiler also known as scanner. It converts the input program into a sequence of Tokens. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.For Example: 1) Keyword
5 min read
Read/Write Structure From/to a File in C For writing in the file, it is easy to write string or int to file using fprintf and putc, but you might have faced difficulty when writing contents of the struct. fwrite and fread make tasks easier when you want to write and read blocks of data.Writing Structure to a File using fwriteWe can use fwr
3 min read
doexec command in Linux with examples doexec command in the Linux system is used to run an executable with an arbitrary argv[0]. It allows the user to argv[0] other than the name of the executable, which is by default passed. Syntax: doexec /path/to/executable argv[0] [argv[1-n]] Options: The argv list is used to send all options to the
1 min read