/* CpSc 824 Sample program demonstrating how to "catch" an alarm timeout. */ #include #include /* Global flag set when timeout occurs */ int timeOutFlag = 0; /* Function called with alarm signal occurs */ void time_limit(int signalCode) { printf("The process has exceeded the time limit\n"); timeOutFlag = 1; return; } main (int argc, char *argv[]) { /* This process should end gracefully in 30 seconds */ alarm(30); /* Set time_limit() as the procedure to invoke when the timeout occurs */ signal(SIGALRM, time_limit); /* Loop waiting for the timeout */ while (!timeOutFlag) { printf("Waiting for timeout\n"); sleep(5); } printf("Loop broken by timeout\n"); }