Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

You can read and write at both ends of the created pipe, but uni-directional means that data only travels in one direction at any time, from parent to child or vice versa. Two pipes are needed for non-blocking sending and receiving of data, meaning that you can read and write at the same time with two pipes, but with one pipe you must finish reading before you can write to the pipe or you must finish writing something before you can read the pipe. In layman terms, you can only read or write at any point of time with only one pipe

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top