/*-------------------------------------------------------------------*/ /* testalarmtimeout.c */ /* The alarm system call to implement a timeout. */ /*-------------------------------------------------------------------*/ #include #include #include #include void mySignalHandler(int iSignal) { printf("\nSorry. You took too long.\n"); exit(1); } int main(int argc, char *argv[]) { int i; signal(SIGALRM, mySignalHandler); printf("Enter a number: "); alarm(5); scanf("%d", &i); alarm(0); printf("You entered the number %d.\n", i); return 0; } /* Sample execution: --> gcc -Wall -ansi -pedantic -o testalarmtimeout testalarmtimeout.c --> testalarmtimeout Enter a number: 123 You entered the number 123. --> testalarmtimeout Enter a number: Sorry. You took too long. */