Qwerto
Qwerto

Reputation: 275

Fork and pipe creation

My book on C applied to Linux, says that if a process creates a child with a fork(), then the pipe created between them follow this principle:

It is important to notice that both the parent process and the child process initially close their unused ends of the pipe

If both processes start with their pipe-end closed, how they know when the other is free to communicate? Maybe, is there an intermediate buffer between the processes?

Upvotes: 1

Views: 56

Answers (2)

kjohri
kjohri

Reputation: 1224

Pipe is an interprocess communication mechanism provided by the kernel. A process writing on the pipe need not worry whether there is some other process to read it. The communication is asynchronous. The kernel takes care of the data in transit.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

Pipes on computers works very much like pipes in real life. There are two ends, you put something into one end and it comes out the other end.

Normally when using pipes in a program, you usually only want the input-end, where you write data, or you want the output-end, where data is read from. If the parent process only wants to write to the child process, and the child process only reads from the parent process, then the parent process could close the read end after the fork, and the child process can close the write end.

Upvotes: 2

Related Questions