CSC 213:  Operation Systems and Parallel Algorithms

Laboratory Exercise on Pipes in [Unix-based/GNU] C

Goals

This laboratory exercise provides practice with several elements of process control in Unix-based C programming.

Preparation

  1. 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.

  2. Read Section 9.3 in Nutt's text on the Pipe Model of interprocess communication.

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, describe, and explain what happens.

  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);
    
    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.

Part B: Local and Global Variables

  1. 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?

  2. Move the assignments
    
    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.

  3. 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 C: Coordination of Specialized Programs

  1. 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    8868
    
    Columns 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:

    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,

Work to be turned in:

Note: For most steps, it is expected that the answer will be short.


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
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at (walker@cs.grinnell.edu)