CSC 213, Fall 2006 : Schedule : Lab 7

Lab 7: Pipes in UNIX

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.


Part A: Experiments with pipe

  1. Copy ~walker/c/concurrency/fork-2.c to your account, compile it with gcc and run it a few times -- waiting a few moments between each run.
  2. In the statement write (fd[1], ...) change the 23 to 10. Then recompile and rerun. How does this affect the running of the program? Briefly explain what you see.

    Now change the 10 (formerly 23) to 30 and rerun. Again, describe the effect and explain why this happens.

  3. In #define change MAX to 15, recompile and rerun. Again describe and explain the result.

  4. With MAX set to 15, change the reading/printing section of code for the child to:
    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 the program.  Describe and explain the result.
  5. With MAX set to 40, change the writing section of code for the parent to:
    write (fd[1], "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26);
    write (fd[1], "abcdefghijklmnopqrstuvwxyz", 26);
    Rerun the program.  Describe and explan the result.
  6. Copy ~walker/c/concurrency/fork-3.c to your account, compile it, and run it. Why do you think the variable value is used in the write statement rather than giving the number 20 directly?
  7. Remove the wait statement from the parent. Then recompile and rerun several times. Does the order of the output ever change? Explain why or why not.

Part B: Coordination of Specialized Programs

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:


Grinnell,9105,8902,8868
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;
field 3 contains the 1990 population; field 4 contains the 1980 population.  I suggest you use 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:

The basic approach for this problem is illustrated in the following diagram:

Main reads; each child analyzes one result

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:

Thus, you will write three (small) programs in all: the parent program; a program to find the city with the largest population decrease; and a program to compute Iowa's rural population.

Work to be Turned In


Janet Davis (davisjan@cs.grinnell.edu)

Created October 8, 2006 based on http://www.cs.grinnell.edu/~walker/courses/213.fa04/lab-pipes.shtml
Last revised October 9, 2006
With thanks to Henry Walker