#include #include #include int flag = 0; void handler(int); int main(int argc, char *argv[]) { char buf[BUFSIZ]; int timeout; /* * Set up a timeout of argv[1] seconds. */ signal(SIGALRM, handler); timeout= atoi(argv[1]); while (1){ alarm(timeout); /* * Prompt for a string and remove the newline. */ printf("Enter a string (withen %d seconds): ", timeout); fgets(buf, sizeof(buf), stdin); buf[strlen(buf)-1] = '\0'; /* * Turn off the alarm, they typed something. */ alarm(0); if (!flag) printf("got %s\n", buf); /* * Display the string we're using. */ } } /* * handler - catch alarm signal and set flag. */ void handler(int sig) { signal(SIGALRM, handler); printf("Timeout Alarm\n"); flag=1; }