/*--------------------------------------------------------------------*/ /* testalarm.c */ /* Author: Bob Dondero */ /*--------------------------------------------------------------------*/ #define _GNU_SOURCE #include #include #include #include #include /*--------------------------------------------------------------------*/ /* This function is intended to be a handler for signals of type SIGALRM. Write a message to stdout that contains iSignal, the number of the signal that caused the function to be called. Then reset the alarm. */ static void myHandler(int iSignal) { /* Really shouldn't call printf() here. See Bryant & O'Hallaron page 766. */ printf("In myHandler with parameter %d.\n", iSignal); /* Reset the alarm. */ alarm(2); } /*--------------------------------------------------------------------*/ /* Demonstrate the alarm() function. As usual, argc is the command-line argument count, and argv contains the command-line arguments. The function never returns. */ int main(int argc, char *argv[]) { const char *pcPgmName; void (*pfRet)(int); pcPgmName = argv[0]; /* Install myHandler as the handler for SIGALRM signals. */ pfRet = signal(SIGALRM, myHandler); if (pfRet == SIG_ERR) {perror(pcPgmName); exit(EXIT_FAILURE); } /* Set a 2 second alarm. After 2 seconds of real time, send a SIGALRM signal to this process. */ alarm(2); /* Enter an infinite loop. */ printf("Entering an infinite loop\n"); for (;;) sleep(1); /* Will not reach this point. */ } /*--------------------------------------------------------------------*/ /* Sample execution: $ gcc217 testalarm.c -o testalarm $ ./testalarm Entering an infinite loop In myHandler with parameter 14. In myHandler with parameter 14. In myHandler with parameter 14. In myHandler with parameter 14. ^C */