Programming Assignment 2
Home   Lectures   Solutions   Handouts   Syllabus      Instructor's Site  
Assignment (Due Wednesday 11 October at 11:00am, 5 percentage points of extra credit toward Friday's exam for finishing by Monday at 11:00am)

  1. Write a program named forkExercise.c. The program should take one command-line argument called maxInteger. If you are uncertain how to use command-line arguments, teach yourself using K&R and the examples in class and in programming assignment 1. forkExercise should begin by forking a child process. The child process should print the odd integers from 1 to maxInteger (inclusive). At the same time, the the parent should print the even integers from 0 to maxInteger (inclusive). For the odd integers, use "%d\n" as the format string for printf. Use "\t%d\n" as the format string for printf for the even integers. This will allow you to easily distinguish the parent and child output. The parent process should wait for the child process to terminate before exiting.
  2.  
  3. Write a program called printOdds.c. The printOdds program should print the odd numbers from 1 to maxInteger (inclusive). maxInteger should, again, be passed as a command-line argument.
  4.  
  5. Write a program named forkExecExercise.c. This program should behave similarly to forkExercise.c, but uses exec to make the child program print the odd integers. The parent should fork one child processes. The child process should exec the printOdds program. The parent process should print the even integers concurrently and then wait for the child process to terminate before exiting. Note that to exec printOdds you will need to create an executable file that you name in the exec statement. Create the executable file by compiling printOdds.c separately and using the gcc -o option to create an executable with the name, printOdds.
Deliverables

The programs forkExercise.c, printOdds.c, forkExecExercise.c. You may earn one percentage point of extra credit toward Friday's exam if you also submit a Makefile that compiles everything. The executable names for each of the programs should be the root filename, i.e., the filename without the .c extension.