CSC 213, Fall 2008 : 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

Note: It is expected that the written answers you give will be short.

  1. Copy ~walker/c/concurrency/fork-2.c to your account, compile it 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 the #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 explain the result.
  6. Copy ~walker/c/concurrency/fork-3.c to your account, compile it, and run it. Why must 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 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. (See fgets(3), strtok(3) and atoi(3) about parsing lines of the file.)

In this part of the lab, you are to write a C program which reads data from this file and determines 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 some data from each line through a pipe to both children. Each child will answer one of the above questions. More precisely:

Note that the child processes should not have any unnecessary file descriptors (or file streams) available in them.

Work to be Turned In

I highly recommend getting a very good start on Part B before leaving for fall break, lest you forget the details. In any case, be sure to comment your code well!

Jerod Weinman

Created June 25, 2008
Based on CSC 213, Fall 2006 : Lab 7 : Pipes in UNIX
With thanks to Janet Davis and Henry Walker