0

I wanted just to implement this algorithm by C on Ubuntu:

wait for a certain time to receive input from keyboard, so, by getting possible input or by over time, the program should be continued.

I dont have any clue to do that! Thank you in advanced.

miryani
  • 11

2 Answers2

0

See the manual for the alarm() and signal() functions. You can easily timeout any code without using any threads or processes.

Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85
  • First of all thank you for reply. I think, in both solutions, run of program goes to wait state. But, my program has a graphic, which should be refreshed every .4 seconds, through this refreshing, a user may enter a key, so now, screen should be refreshed, to show the user's impact. – miryani Jan 28 '12 at 05:07
0

The common way to do that is with select() or poll():

struct pollfd fd = {STDIN_FILENO, POLLIN};
switch(poll(&fd, 1, 1)){
case -1:
     die("poll failed");
     break;
case 0:
     //timed out...
     break;
default:
     //read from stdin 
}    
Dave
  • 10,964
  • 3
  • 32
  • 54
  • First of all thank you for reply. I think, in both solutions, run of program goes to wait state. But, my program has a graphic, which should be refreshed every .4 seconds, through this refreshing, a user may enter a key, so now, screen should be refreshed, to show the user's impact. – miryani Jan 28 '12 at 05:07