next up previous
Next: Build a looper Up: Assignment 5: Processor Scheduling Previous: Exercises

Build a time bomb

1.
Write a program called alarm that executes a mathematically intensive loop. Make the inner loop do something interesting like floating-point division or square roots.
2.
Make your program take an argument. The following code fragment shows how to get arguments from the command line:

int main (int argc, char *argv[])
{
  int num_children;

  if (argc == 2) {
    num_children = atoi (argv[1]);
  } else {
    printf ("Usage: fork number_of_children\n");
    exit (-1);
  }
}

This is from the fork program I used two assignments ago. To execute this program, you type fork 16 on the command line and it forks 16 children. If you have the wrong number of arguments, it prints a ``Usage'' message telling you what the arguments are supposed to be. This is a fairly standard interface for UNIX programs.

3.
For alarm, the argument should be the number of seconds the loop should continue. You can use the alarm system call to run a program for a given length of time and then quit. Check out the man page for alarm.

4.
Run your program with various command-line arguments and confirm that it quits after the appropriate amount of time. You can use the time command as in the last assignment, or you can use the
\time
command instead, which prints slightly different information about your process (thanks to Peter and Jen who discovered this apparently undocumented feature by accident).


next up previous
Next: Build a looper Up: Assignment 5: Processor Scheduling Previous: Exercises
Allen B. Downey
3/17/1998