CSC 213, Fall 2008 :
Schedule : Lab 4
Lab 4: Signals and Timers
Goals: This lab exercise provides experience with system timers and
signals.
Reading:
-
Lab 6.1 (Nutt, pp. 232-236) provides general background on timers and
outlines a framework for the use of signals and timers in programs.
-
signal-1.c
(with timers.h
and timers.c, an
example of using signals and timers). We will go over these programs
at the beginning of our lab meeting.
Collaboration: You will complete this lab in teams,
as assigned by the instructor.
Basic Problems:
-
Determine the time required to perform a quicksort in both a
single-process and multiple-process context.
-
Use signal handlers for coordination and user interaction.
Guidelines:
-
The collection of timings in Part A should use three timers:
-
ITIMER_REAL is used to determine overall elapsed time,
-
ITIMER_VIRTUAL is used to determine time for the
specific process, and
-
ITIMER_PROF is used to determine time for the
specific process plus time of the OS related to this process.
-
You may use either your own implementation of quicksort from Lab
1 or one from the examples
directory. Either implementation should be properly
attributed.
-
The quicksort should be performed on an array of random data.
Part A
In 1 and 2 below, you will operate quicksort on random integer arrays of sizes
100000, 200000, 400000, 800000, 1600000.
-
As a base line, a first program should set the three timers
indicated above, perform a quicksort, and retrieve processing
times in a single-process environment. This program
should not include any fork or signal calls. Also,
timing should not include the time required to initialize the
array.
This program may perform the timing, quicksort, and printout for
a single array size, with the program rerun for each of the array
sizes given above. Alternatively, the program may call the timing,
quicksort, and printout steps within a loop to handle all array sizes
with a single execution of the program.
-
Modify signal-1.c, so that
the child process computes the amount of time required for each
quicksort that it performs. The parent process should do no work,
but should print its own timings for each period of time that the
child spends computing. In other words, both the parent and the
child should run all three timers for the quicksort. As with the
baseline program, timing should not include the time
required to initialize arrays.
Part B
In this part, you should sort one very large array (it should take at
least 45 seconds to sort).
Note: you may need to
use malloc(2) to allocate this array from the heap, since it
will likely be too big to be allocated from the stack.
-
Modify program A.1 to include two special signal handlers.
-
Set a signal handler that asks the user if they want to keep
sorting or quit when a Ctrl-C is given at the terminal
(SIGINT).
-
Use a timer to interrupt regularly (say, 5 seconds) to ask if
the user wants to keep sorting or quit (SIGALRM).
Your user interface need not be extravagant or "overly" robust. It
need only work correctly when the user enters properly formatted
input, taking some default action otherwise. You may use any of
the I/O functions you choose.
Update:
Your modified program does not need to use any timers to keep
track of the total time for the sort, as in A.1. You need only implement
the signal handlers and set an appropriate timer.
-
SIGINT handler behavior:
- What happens when you type Ctrl-C once?
-
What happens when you type Ctrl-C twice (ie, the second Ctrl-C
happens while inside your signal handler)? Give your explanation.
-
What would you like to happen if someone typed Ctrl-C while in
your signal handler? Why? Explain how you think you could make
this happen.
-
SIGALRM handler behavior:
-
What happens when it takes longer than the timer length to
answer (i.e., what happens after you finally do answer?) Give
your explanation.
-
What would you like to happen when it takes someone longer
than the timer length to answer? Why? Explain how you think
you can make this happen.
Work to be turned in:
- Part A: programs 1 and 2, as described above. Due Friday 9/26.
-
Part B: program 3, as described above, and answers to questions in
4 and 5. Due Monday 9/29.
You may turn in both part A and part B on Friday, 9/26 if you prefer.
Follow the submission guidelines for both parts A and B. For each
program, use the script command to list the program and to execute
one or more test runs for the required array sizes. In your
commentary explain any differences in results that you see for
different array sizes and configurations of processes.
Jerod Weinman
Created June 24, 2008
Based on
CSC
213, Fall 2006 : Lab 4: Signals and Timers
Last revised June 24, 2008
With thanks to Henry Walker and Janet Davis.