In Chapter 6 we looked at using redirection and a pipe to have one process send information to another.
We will develop the ideas there into having many processes communicate using pipes.
The process will communicate in the form of a ring:
Figure 7.1 (page 226): Unidirectional ring with five nodes.
A Ring of One Process
Example 7.1 and Figures 7.2 through 7.4 show how to create a ring of one process.
Figure 7.2 (page 228): Status of the process of Example 7.1 after pipe(fd) has been executed.
Figure 7.3 (page 228): Status of the process of Example 7.1 after both dup2(fd)s have been executed.
Figure 7.4 (page 229): Status of the process at the end of Example 229.
Exercises 7.2 through 7.4 explore communication in the ring of 1 process.
What is the output generated by each of the these exercises?
Exercise 7.2: Outputs the integers 0 through 9
Exercise 7.3: Hangs on the first read
Exercise 7.4: Hangs on the first scanf because printf(...)
is really fprintf(stdout,...) and this uses file pointers so it
is buffered.
Use fflush(stdout) after the printf.
Note that output to standard error is not buffered.
A Ring of Two Processes
Example 7.5 (page 230) shows how to create a ring of 2 processes.
Figures 7.5 through 7.8 trace this program segment.
Figure 7.5 (page 230): Connections to the parent process of Example 7.5 after the second pipe(fd) has been executed.
Figure 7.6 (page 321): Connections to the processes of Example 7.5 after the fork but before the rest of the if statement. Process A is the parent and Process B is the child.
Figure 7.7 (page 232): Connections to the processes of Example 7.5 after the if statement has been executed. Process A is the parent and Process B is the child.
Figure 7.8 (page 232): Connections to the processes of Example 7.5 after the conde segment has been executed. Process A is the parent and Process B is the child.
A Ring of n Processes
Look at Program 7.1, Page 233.
What would happen if: