#include /* standard I/O functions */ #include /* standard unix functions, like alarm() */ #include /* signal name macros, and the signal() prototype */ char user[40]; /* buffer to read user name from the user */ /* define an alarm signal handler. */ void catch_alarm(int sig_num) { printf("Operation timed out. Exiting...\n\n"); exit(0); } int main(int argc, char* argv[]) { /* set a signal handler for ALRM signals */ signal(SIGALRM, catch_alarm); /* prompt the user for input */ printf("Username: "); fflush(stdout); /* start a 15 seconds alarm */ alarm(15); /* wait for user input */ gets(user); /* remove the timer, now that we've got the user's input */ alarm(0); printf("User name: '%s'\n", user); return 0; }