Prerequisite : I/O System calls
Conceptually, a pipe is a connection between two processes, such that the standard output from one process becomes the standard input of the other process. In UNIX Operating System, Pipes are useful for communication between related processes(inter-process communication).
- Pipe is one-way communication only i.e we can use a pipe such that One process write to the pipe, and the other process reads from the pipe. It opens a pipe, which is an area of main memory that is treated as a “virtual file”.
- The pipe can be used by the creating process, as well as all its child processes, for reading and writing. One process can write to this “virtual file” or pipe and another related process can read from it.
- If a process tries to read before something is written to the pipe, the process is suspended until something is written.
- The pipe system call finds the first two available positions in the process’s open file table and allocates them for the read and write ends of the pipe.
int pipe(int fds); Parameters : fd will be the fd(file descriptor) for the read end of pipe. fd will be the fd for the write end of pipe. Returns : 0 on Success. -1 on error.
Pipes behave FIFO(First in First out), Pipe behave like a queue data structure. Size of read and write don’t have to match here. We can write 512 bytes at a time but we can read only 1 byte at a time in a pipe.
hello, world #1 hello, world #2 hello, world #3
Parent and child sharing a pipe
When we use fork in any process, file descriptors remain open across child process and also parent process. If we call fork after creating a pipe, then the parent and child can communicate via the pipe.
Output of the following program.
hello world, #1 hello world, #2 hello world, #3 (hangs) //program does not terminate but hangs
Here, In this code After finishing reading/writing, both parent and child block instead of terminating the process and that’s why program hangs. This happens because read system call gets as much data it requests or as much data as the pipe has, whichever is less.
- If pipe is empty and we call read system call then Reads on the pipe will return EOF (return value 0) if no process has the write end open.
- If some other process has the pipe open for writing, read will block in anticipation of new data so this code output hangs because here write ends parent process and also child process doesn’t close.
For more details about parent and child sharing pipe, please refer C program to demonstrate fork() and pipe().
This article is contributed by Kadam Patel. If you like GeeksforGeeks and would like to contribute, you can also write article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Accept system call
- Wait System Call in C
- dup() and dup2() Linux system call
- Difference between Call by Value and Call by Reference
- C program to demonstrate fork() and pipe()
- Named Pipe or FIFO with example C program
- A C/C++ Function Call Puzzle
- Can we call an undeclared function in C++?
- Difference between CALL and JUMP instructions
- Is it possible to call constructor and destructor explicitly?
- How to call function within function in C or C++
- system() in C/C++
- Amazing stuff with system() in C / C++
- How does Volatile qualifier of C works in Computing System
- Getting System and Process Information Using C Programming and Shell in Linux
- Input-output system calls in C | Create, Open, Close, Read, Write
Improved By : Akanksha_Rai