![]() |
VOOZH | about |
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).
int pipe(int fds[2]);Parameters :fd[0] will be the fd(file descriptor) for the read end of pipe. fd[1] 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
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.
Output
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.
For more details about parent and child sharing pipe, please refer C program to demonstrate fork() and pipe()