FIFO or named Pipe
I already explained about pipe in tutorial Pipe in Linux. FIFO or unnamed pipe is same as pipe or named pipe except for one thing. Pipe is a one way communication between process which is related to each other like parent and child processes. Related processes have same file descriptor, so they communicate easily through single pipe but unrelated processes have different file descriptors, so they cannot communicate through single pipe. You can communicate only through creating multiple pipes each for one communication. Communication between multiple processes is not sufficient and hence FIFO or named pipe overcomes this limitation.
FIFO (First In First Out) is first out file which is like normal file but doesn’t save the content of file, it should pass data, write or read by first in and first out process. When one process is read or written using FIFO, another process waits to access FIFO. So FIFO can also be used for one-way communication between multiple and unrelated processes instead of single and related process. It creates file inode in virtual file system same as normal file with similar characteristics. During communication if reading or writing of any process is stopped or closed, it’s called broken pipe. It is simple, if names are given to pipe, then one would be able to read or write data to them just like a normal file.
You can communicate between unrelated processes through FIFO or named pipe by shell command. To create named pipe, you can use following command from terminal:
Syntax:
mkfifo file_name
Here, file_name is indicating the name of pipe.
You can use another way to create FIFO or named pipe by following command:
mknode file_name p
Here, file_name indicates that the name of pipe and p indicates of file type named pipe.
Let’s see one example of how to communicate two processes through named pipe.
I want to convert text file to compressed file through named pipe. Suppose I have text file named pipe.txt.
First I will create FIFO or named pipe using mkfifo command.
For example,
mkfifo pipe_fifo
Similarly I can create FIFO or named pipe using mknod command.
For example,
mknod pipe_fifo p
You can create named pipe using any method as I explained here.
After creating named pipe, you can use it to communicate between two processes. Now, you will be able to share data between the two processes.
I can share the data of pipe.txt file with FIFO by following line:
cat pipe.txt > pipe_fifo
Here content of pipe.txt is shared with named pipe pipe_fifo. File pipe.txt writes data into pipe_fifo file.
After pressing ENTER key, process is in running mode, so you cannot enter another command from command terminal. Open another command terminal for reading data from pipe_fifo and convert it into compressed file. We can read from pipe_fifo and convert it into compressed file (i.e. ZIP File) by following command line:
gzip –q –c < pipe_fifo > pipe_compressed.gz
Now pipe.txt file is converted into zip file named pipe_compressed.gz. As soon as you enter command in second terminal the process stops and exits from running mode.
Note: when you open another terminal, do not close first running terminal.
In next tutorial, I will explain how to create named pipe or FIFO in C programming.
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.