CSC 357 Lab 6
Getting Started with
fork and exec



ISSUED: Monday, 14 May 2007
DUE: On or before 11:59:59PM Monday 21 May, via handin on falcon/hornet
POINTS POSSIBLE: 100
WEIGHT: 2% of total class grade
READING: Stevens Chapter 8, Lecture Notes Week 7, appropriate man pages

Tasks:

  1. Write a program named f-test.c. The program takes a single integer, N, as a command-line argument. It starts by forking a child process. The child prints the odd numbers from 1 to N (inclusive) while the parent prints the even numbers from 1 to N (inclusive). Neither the parent nor the child waits for the other to start printing. When printing the odd numbers, use "%d\n" as the format string for printf. For the even numbers, use "\t%d\n" as the format string for printf. This allows parent and child output to be easily distinguished. When the child process finishes printing, it exits. When the parent process finishes printing, it properly waits for the child process to terminate. "Properly" wait means the parent uses the waitpid function to wait for the child, and exits normally if WIFEXITED(status) is true for the status value returned from waitpid. Exiting "normally" means the parent program exits with value EXIT_SUCCESS, without any further output to stdout. If WIFEXITED(status) is false, the parent prints the message "abnormal child termination" to stderr, and exits with EXIT_FAILURE.

  2. Write two programs odds.c and evens.c. The odds program prints the odd numbers from 1 to N (inclusive). The evens program prints the even numbers from 1 to N (inclusive). Use the same indentation convention as above, namely indent the even numbers with one leading tab.

  3. Write a program named fe-test.c. This program behaves similarly to the program from the first task, but uses exec to execute the programs written for the second task. The parent forks two child processes. One child process calls exec of the odds program. The other calls exec on the evens program. Neither child waits for the other to start printing. The parent process properly waits for both child processes to terminate, but allows them to execute concurrently.

Discussion

What you should observe with these programs is the race condition on stdout. Namely, when two processes run concurrently, the order in which they get access to stdout is not defined. Also undefined is the amount of output that one process performs before the other process takes over.

This race-condition behavior is easiest to observe with a reasonably large value of the command-line argument N, say 500 or 1000. With values this large, the process that starts first will most likely not have a chance to finish printing before the other process starts. This will show up in the output by not having all of the odds or evens printed consecutively, without some interleaving of evens or odds.

Deliverables

The programs f-test.c, odds.c, evens.c, fe- test.c, and a Makefile to compile everything. The executable names for each of the programs should be the root filename, i.e., the filename without the .c extension.

Scoring Details

40 points for f-test; 60 points for fe-test (including odds and evens).

Collaboration Allowed

Collaboration with a lab partner IS allowed, but each partner must turn a copy of the work.

How to Submit the Deliverables

Submit the programs and Makefile via handin.


index | lectures | labs | programs | handouts | solutions | examples | documentation | bin