This laboratory exercise provides practice with several elements of process control in Unix-based C programming.
Use of pipes to allow interprocess communication with pipe
and popen.
Elements of global and local variables within the context of parent and child processes.
Problem solving by connecting small, specialized programs.
Review sample programs fork-2.c through fork-6.c
in
An Introduction to Concurrency in Unix-based [GNU] C
Through Annotated Examples, as discussed in class.
Read Section 9.3 in Nutt's text on the Pipe Model of interprocess communication.
Now change the 10 (formerly 23) to 30 and rerun. Again, describe the effect and explain why this happens.
read(fd[0], line, MAX);
printf ("The string received is '%s'\n", line);
read(fd[0], line, MAX);
printf ("The string received is '%s'\n", line);
That is, perform the reading and printing twice. Rerun, describe, and
explain what happens.
write (fd[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26); write (fd[1], "abcdefghijklmnopqrstuvwxyz", 26);At the same time, restore the code for the child to contain just one read of line with a single corresponding printf. Rerun, describe, and explain the result.
data_g = 10; /* change data elements */ data_l = 12;from the beginning of the code for the parent process to the beginning of the child's code segment. Recompile and rerun. Compare this output with that from the previous step (step 8), and explain any differences.
Consider the following problem, which is based on an exercise by John Stone:
The file /home/walker/213/labs/Iowa-cities.dat contains information about the sixty largest cities and towns in Iowa: their names and populations, as determined by the 2000, 1990, and 1980 censuses. A typical line of the file looks like this:
Grinnell 9105 8902 8868Columns 1 through 16 contain the name of the town, left-justified; columns 17 through 22 contain the 2000 population, right-justified; columns 23 and 24 are always spaces; and columns 25 through 30 contain the 1990 population, right-justified; columns 31 and 32 are always spaces; and columns 33 through 38 contain the 1980 population, right-justified.
In this step, you are to write C programs which read data from this file and determine the answers to the following two questions:
Which of these Iowa city had the largest percentage decrease in population between 1980 and 2000?
The basic approach for this problem is illustrated in the following diagram:
As this diagram suggests, one parent process will spawn two children; the parent and both children then should collaborate to perform this work. Overall, the parent process will read the file and send a copy of each line through a pipe to each child. Each child will answer one of the above questions. More precisely,
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/213.fa04/lab-pipes.shtml
|
created September 20, 1998 last revised October 13, 2004 |
|
| For more information, please contact Henry M. Walker at (walker@cs.grinnell.edu) |