/*------------------------------------------------------------------*/ /* testalarm.c */ /* The alarm() function */ /* Author: Bob Dondero */ /*------------------------------------------------------------------*/ #define _GNU_SOURCE #include #include #include #include /*------------------------------------------------------------------*/ static void myHandler(int iSig) /* This function is intended to be a handler for signals of type SIGALRM. Print iSig to stdout, and reset the alarm. */ { printf("In myHandler with argument %d\n", iSig); /* Reset the alarm. */ alarm(2); } /*------------------------------------------------------------------*/ int main(void) /* Illustrate the alarm() function. Return 0. */ { void (*pfRet)(int); sigset_t sSet; int iRet; /* Make sure that SIGALRM is not blocked. */ sigemptyset(&sSet); sigaddset(&sSet, SIGALRM); iRet = sigprocmask(SIG_UNBLOCK, &sSet, NULL); assert(iRet == 0); /* Register myHandler as the handler for SIGALRM signals. */ pfRet = signal(SIGALRM, myHandler); assert(pfRet != SIG_ERR); /* Set a 2 second alarm. After 2 seconds of real time, send a SIGALRM signal to this process. */ alarm(2); printf("Entering an infinite loop\n"); for (;;) ; return 0; }