alarm() and SIGALRM
Script started on Fri May 02 12:58:08 1997
sh-2.00$ cat alarm.c
/* alarm.c - demonstrate the SIGALRM signal and alarm system call */
#include <stdio.h>
#include <signal.h>
void wakeup(int); /* executed when alarm received. */
void cleanup(int); /* Executed upon an abort. */
int counter = 0; /* Global so wakeup() and main() can both see it. */
void wakeup(int signum)
{
/* print the current value of counter */
system("date");
printf("counter is currently %d.\n ",counter);
printf("\n");
/* Set signal to this function. */
signal(SIGALRM, wakeup);
/* Set alarm clock for 5 seconds. */
alarm(5);
}
void cleanup(int signum)
{
printf("Exiting after abort received.\n\n");
exit(1);
}
void main()
{
/* Illustrate catching interactive abort signals. */
signal(SIGINT, cleanup); /* Normally Delete key */
signal(SIGQUIT,cleanup); /* Normally ^\ */
/* Quietly count to ourselves but every 5 sec or so output
the counter. */
wakeup(0);
while(1)
counter++;
}
sh-2.00$ alarm
Fri May 2 12:58:50 CDT 1997
counter is currently 0.
Fri May 2 12:58:55 CDT 1997
counter is currently 76376094.
Fri May 2 12:59:00 CDT 1997
counter is currently 151521801.
Fri May 2 12:59:05 CDT 1997
counter is currently 226428907.
Exiting after abort received.
exit
script done on Fri May 02 12:59:29 1997