CS520
Spring 2015
Laboratory 9
Monday April 6
The lab must be submitted prior to 8am on Tuesday April 7.
The goal of this lab is to learn how to use timers,
signals and signal handlers.
- Create a program called lab9.c that does the following:
- Place the following line first in the file. There can be comments
before it but nothing else.
#define _GNU_SOURCE
- Include the following files: stdio.h,
stdlib.h,
sys/time.h and signal.h.
- Initialize a global counter to zero.
- Set up a handler function for the SIGVTALRM signal.
A handler function is a void function that has a single int
parameter, which is the number of the signal that caused it
to be called. When called, the handler function should simply increment
the global counter. Request that your handler function be called for
SIGVTALRM signals by using the library routine
sigaction:
- The first parameter to sigaction should be
SIGVTALRM.
- The second parameter should be the address of a
sigaction struct:
- The sa_handler member of this struct
should be set to the address of your handler function.
- The sa_mask member should be set to the
empty set, by passing the address of the member to
the sigemptyset library routine.
- The sa_flags member should be set to
SA_NODEFER.
- The third parameter should be NULL.
- Initialize a timer to generate a signal every 10 milliseconds
by using the setitimer library routine:
- The first parameter to setitimer should be
ITIMER_VIRTUAL.
- The second parameter should be the address of an
itimerval struct:
- The it_interval member of this struct
should have its tv_sec member set to zero
and its tv_usec member set to 10000.
- The it_value member of this struct should
have its tv_sec member set to zero and its
tv_usec member set to 10000.
- The third parameter should be NULL.
- Create a loop that simply does nothing while waiting for the
global counter to reach 100. When the counter reaches 100, then
the program should exit.
- Be sure to test the return value of all library calls.
Use the perror library routine if an error is
encountered.
- Consult the man pages for the library routines for more
information about them.
- Test your program.
Turn in this laboratory by typing:
% ~cs520/bin/submit lab9 lab9.c
Submissions can be checked by typing:
% ~cs520/bin/scheck lab9
Remember that there are no late submissions of labs.
Submit what you have prior to 8am on Tuesday April 7.
Last modified on April 2, 2015.
Comments and questions should be directed to
hatcher@unh.edu