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.

  1. Create a program called lab9.c that does the following:

    1. Place the following line first in the file. There can be comments before it but nothing else.

             #define _GNU_SOURCE
             

    2. Include the following files: stdio.h, stdlib.h, sys/time.h and signal.h.

    3. Initialize a global counter to zero.

    4. 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:

      1. The first parameter to sigaction should be SIGVTALRM.

      2. The second parameter should be the address of a sigaction struct:

        1. The sa_handler member of this struct should be set to the address of your handler function.

        2. The sa_mask member should be set to the empty set, by passing the address of the member to the sigemptyset library routine.

        3. The sa_flags member should be set to SA_NODEFER.

      3. The third parameter should be NULL.

    5. Initialize a timer to generate a signal every 10 milliseconds by using the setitimer library routine:

      1. The first parameter to setitimer should be ITIMER_VIRTUAL.

      2. The second parameter should be the address of an itimerval struct:

        1. The it_interval member of this struct should have its tv_sec member set to zero and its tv_usec member set to 10000.

        2. The it_value member of this struct should have its tv_sec member set to zero and its tv_usec member set to 10000.

      3. The third parameter should be NULL.

    6. 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.

  2. Be sure to test the return value of all library calls. Use the perror library routine if an error is encountered.

  3. Consult the man pages for the library routines for more information about them.

  4. 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