Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cant a pipe created using pipe() be used as a bi-directional pipe?

Almost all the pipe examples I've seen advice closing the unused write/read ends. Also man clearly states that pipe() creates a pipe, a unidirectional data channel But I've tried reading and writing to both ends of the pipe in both the parent and the child and everything seems to be OK.

So my doubt is why do we need 2 pipes if two processes have to both read and write to each other and why not do it using a single pipe?

like image 465
Pavan Manjunath Avatar asked Dec 13 '22 03:12

Pavan Manjunath


1 Answers

If you use the same pipe how does the child separate its messages from the parents messages and vice versa?

For example:

Parent writes to pipe
Parent reads from pipe hoping to get message from child but gets its own message :(

It is much easier to use one pipe for child->parent and another pipe for parent->child.

Even if you have some protocol for reading/writing it is quite easy to deadlock the parent and child process.

like image 110
benmmurphy Avatar answered Dec 27 '22 21:12

benmmurphy