Pipe in Linux
Various processes work in Linux system out of which some are dependent to each other or in other words one process uses some data of another process. Linux has a communication mechanism to communicate between two different processes called Pipe.
Pipe is one way communication of data between one process to another process. it is a form of communication called Interprocess communication (IPC). Pipe transfers the output of one process to another which is accepted as an input. In computer, various processes run at the same time and require some data of another process or depend on another process. So, this data sharing between the processes is an important element of Linux.
All information transferred by process to pipe passes to another process via kernel.. In Linux command shell, pipe is created by simple vertical bar operator (|) between two commands. Operator ( | ) is a special operator which joins the output of one process with input of another process.
$ ls | more
Here output of ls command is directly redirected with pipe and second process (execution of more) reads from pipe as its input. Here first one reads the process and another one writes it.
Fig. 1: Overview of Is Command in Linux
It looks like creating temporary file but is more convenient. For Example,
$ ls > temp
$ more > temp
It is same as creating pipe but a temporary file consumes unnecessary size.
It is half duplex pipe. So, each process must be close one before using another (process want to use pipe). Two-way communication between processes is possible by creating two pipes between process one for each direction. But both processes require same parent ID which is a limitation of pipe Interporcess communication. One pipe can communicate between two associate processes but cannot communicate with more than one process. If two or more processes want to read or write the same pipe, they must synchronize their accesses by using semaphores or file locking.
Fig. 2: Image showing Half Duplex Pipe in Linux
Pipe seems like a file but it isn’tand no file pointer is associated with it. It has a fixed number of logical blocks which can be emptied or filled. This type of pipe is called unnamed pipe because it is not available or located anywhere in the file. Pipe has a limited size of logical block and removes the data, which are read by another process. Unnamed pipe is closed after closing the process.
Pipe is created by pipe ( ) system call. Pipe creates two file descriptors. First file descriptor (i.e., fd ) reads the data from pipe and second file descriptor (i.e. fd  ) writes the data into pipe. I will explain how to create a pipe in Linux in next tutorial.
Filed Under: Tutorials