/*-------------------------------------------------------------------*/ /* testalarm.c */ /* The alarm system call. */ /*-------------------------------------------------------------------*/ #include #include #include #include /*-------------------------------------------------------------------*/ void mySignalHandler(int iSignal) /* This function is intended to be a handler for signals of type SIGALRM. Print iSignal to stdout, re-register this function as the handler for signals of type SIGALRM, and reset the alarm. */ { void (*pfRet)(int); printf("In mySignalHandler with argument %d\n", iSignal); /* Re-register the signal handler. */ pfRet = signal(SIGALRM, mySignalHandler); assert(pfRet != SIG_ERR); /* Reset the alarm. */ alarm(3); } /*-------------------------------------------------------------------*/ int main(int argc, char *argv[]) /* Illustrate the alarm system call. */ { void (*pfRet)(int); /* Register mySignalHandler as the handler for SIGALRM signals. */ pfRet = signal(SIGALRM, mySignalHandler); assert(pfRet != SIG_ERR); /* Set a 3 second alarm. After 3 seconds of real time, send a SIGALRM signal to this process. */ alarm(3); printf("Entering an infinite loop\n"); for (;;) ; return 0; } /* Sample execution: $ gcc -Wall -ansi -pedantic -o testalarm testalarm.c $ testalarm Entering an infinite loop In mySignalHandler with argument 14 In mySignalHandler with argument 14 In mySignalHandler with argument 14 ... ^C */