IPC Technique — PIPE
Pipe :
- The oldest mechanism for IPC is Pipes.
- From the Day one we have been using “ | “ . now we will see what actually goes on when we use this ”|”
- Pipes are like Fifo without the name.
- Pipes follow FIFO (first in first out) rule .
- A FIFO buffer is like a queue ,Elements are added at one side of the queue and exit from the another end of the Pipe.
- There is no chance of overtake.
- Pipe is unidirectional and local to the system.
Creating Pipe Using System Call:
- pipe() system call is defined in unistd.h header file.
int pipe(int pipefd[2]);
- pipe() creates a pipe, a unidirectional data channel that can be used for inter process communication.
- The array pipefd is used to return two file descriptors referring to the ends of the pipe.
- pipefd[0] refers to the read end of the pipe.
- pipefd[1] refers to the write end of the pipe.
- Data written to the write end of the pipe is buffered by the kernel until it is read from the read end of the pipe.
- Initially:
- Problem:
The array is declared in a Process and fd(s) are going to be in single process.
- Solution:
- After using Fork() and execl()
Process 1 Process 2
Some Important Facts On Pipe
- Situation 1: For the communication to happen through pipe.
Both Reader and Writer has to be connected to the pipe at the same time.otherwise SIGPIPE signal will terminate the
process.
- Situation 2: If the reader process and writer process both connected to the Pipe. But the Writer is not writing into the Pipe (obviously Pipe is Empty),
In this situation , the reader will notice that the writer is present But the pipe is Empty , then the reader will go on
BLOCK-ON-READ. The reader will wait till the writer writes into pipe.
- Situation 3: If the reader process and writer process both connected to the Pipe. And the Writer writes 64kb into pipe.
- the reader is not reading anything ,now in this situation the Pipe gets full. And the Writer will go on BLOCK-ON-WRITE.
- The Writer will stays on BLOCK-ON-WRITE mode till reader reads something from pipe.
- Situation 4:
- ? if Parent closed the read file descriptor of the pipe .will the child Process be able to read ?
- Yes, the child Process will be able to read .because parent and child both have different read fd(s).
- Situation 5:
? The reader tries to read from the pipe But the writer does not write anything and close its pipe fd . Then What will happen?
- The reader will not read anything due to empty pipe , it tried and tried and goes on BLOCK-ON-READ .
- But if writer closed its fd then a SIGPIPE signal generate and it will terminate the Process.