Pipe with fork
pipe_creation.c
shows you the basic operation on pipe. But it is not very useful for a single process to use a pipe to talk to itself. In typical use, a process creates a pipe just before it forks one or more child processes. The pipe is then used for communication either between the parent or child processes, or between two sibling processes(A2).pipe_withfork.c
andpipe_withfork2.c
show you how pipe works between related process.
Inpipe_withfork.c
, parent writes "CSCI3150" to the pipe, child read from the pipe 1 byte at a time until the pipe is empty.
Analysis: As you can see, the pipe works correctly. After fork(), pipe file descriptors are shared between the parent and child, as show in the picture below.
To ensure pipe work properly, you should:*Always be sure to close the end of pipe you aren't concerned with*.That is, if the parent wants to receive data from the child, it should close pipefds[1], and the child should close pipefds[0]. When processes finish reading or writting, close related file descriptors. Otherwise, there will be undesired synchronization problems.
pipe_withfork2.c is almost the same with pipe_withfork.c except line * is commented out. That means, child forget to close write end. What will happen?
Analysis:
We can see that the child process doesn't exit. What's going on? Note that the child process still has pipefds[1] open. The system will assume that a write could occur while any process has the write end open, even if the only such process is the one that is currently trying to read from the pipe, and the system will not report EOF(A4), therefore. Thus child is waiting to read from pipefds[0], and the operating system doesn't know that no process will be writing to pipdfds[1]. So the child process blocks until data arrives to be read (A3)
Last updated
Was this helpful?