i have want to simple program which will do the following->
1. Display enter the name message
2. Accept input for 5 secs
3. if user is successful to write message in 5 secs then he wins
4. else looses.
I want to do it with the use of alarm() system call on unix.
Code:#include<stdio.h> #include<sys/signal.h> #include<unistd.h> char *name; void wakeup() { scanf("%s",name); } int main() { while(1) { printf("Enter the name :->"); signal(SIGALRM,wakeup); alarm(5); pause(); } return 0; }
Problems with the code:
1. It is not following the flow, seems that pause() has more priority. The message ""Enter the name :->", is displayed with delay.
2. whenever control goes to wakeup, scanf blocks the control for infinite period. I mean unless I press ENTER, control wont do anything. So whole idea of terminating the input process after 5 secs becomes useless
how to do it???