Goals: This laboratory exercise provides practice with the use of pipes to allow interprocess communication, and the strategy of problem solving by connecting small, specialized programs.
Preparation: Read through sample programs fork-2.c through fork-6.c in An Introduction to Concurrency in Unix-based [GNU] C Through Annotated Examples.
Collaboration: You will complete this lab in teams of 1 to 3 of your choice You may, as always, consult with your classmates on issues of design and debugging.
pipeNow change the 10 (formerly 23) to 30 and rerun. Again, describe the effect and explain why this happens.
In #define change MAX to 15, recompile and rerun. Again describe and explain the result.
read(fd[0], line, MAX);That is, perform the reading and printing twice. Rerun the program. Describe and explain the result.
printf ("The string received is '%s'\n", line);
read(fd[0], line, MAX);
printf ("The string received is '%s'\n", line);
write (fd[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26);Rerun the program. Describe and explan the result.
write (fd[1], "abcdefghijklmnopqrstuvwxyz", 26);
Consider the following problem, which is based on an exercise by John Stone:
The file /davisjan/csc/213/examples/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:
There are 4 fields separated by commas. Field 1 is the name of the town (at most 16 characters); field 2 contains the 2000 population;
Grinnell,9105,8902,8868
fscanf to parse lines of the file.
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 cities 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:
popen to create one child process.
The parent then should send formatted data to that child using the file
descriptor returned by popen.
popen) should read from standard
input and determine the city with the greatest percentage
decrease in population.
exec a new program
that reads from standard in and computes the rural population
directly.Janet Davis (davisjan@cs.grinnell.edu)
Created October 8, 2006 based on http://www.cs.grinnell.edu/~walker/courses/213.fa04/lab-pipes.shtml