For the timer question, this depends on what you need.
There is the sleep(seconds) system call, but it will not let you sleep for less than 1 second.
Another alternative may be to use select() with only the timeout parameter filled in, though this won\'t let the program do anything in parallel, it\'ll just let go of the CPU for what you set in timeout, so that other processes can use the CPU.
Therefore you may wish to look into timers (man setitimer).
You will also have to look into signals for this to work.
This is a quick example I ripped off a program of mine sloppily adapting it to fit what you seem to need.
Without the timer it will be simpler, though:
#include //setitimer,etc.
#include
#include
int main(void)
{
unsigned int timeouts;
struct itimerval itv;
int exit_loop=0;
itv.it_interval.tv_sec=MY_TIMEOUT_SEC;
itv.it_interval.tv_usec=MY_TIMEOUT_USEC;
while (!exit_loop)
{
selectresult=select(0,NULL,NULL,NULL,&itv);
...
}
}
Example with timer:
#include //setitimer,etc.
#include
#include
unsigned int timeouts;
struct itimerval itv;
void MY_SIGALRM_Handler(int signal)
{
timeouts++;
}
int Reset_Timer(void)
{
itv.it_value=itv.it_interval;
return setitimer(ITIMER_REAL,&itv,NULL);//start timer
}
int main(void)
{
int exit_loop=0;
sigset_t MYSignalMask;
sigemptyset(&MYSignalMask);
struct sigaction target;
target.sa_handler=MYSIGALRM_Handler;
target.sa_mask=MYSignalMask;
target.sa_flags=SA_RESTART;
target.sa_restorer=NULL;
sigaction(SIGALRM,&target,NULL);
itv.it_interval.tv_sec=MY_TIMEOUT_SEC;
itv.it_interval.tv_usec=MY_TIMEOUT_USEC;
Reset_Timer();
while (!exit_loop)
{
selectresult=select(0,NULL,NULL,NULL,NULL);
...
}
}
As I said... sloppy, as I\'m on PS ATM, but that\'s reasonably what it should look like. You get the idea, anyway.
Edit: For the second question: this obviously differs depending on what platform you use. In Linux, on the console, using (n)curses you\'d need to do erase() to clear the entire screen, or use mvprintw() to overwrite the screen at the specified position.
On the windoze console, it is called clrscr() and gotoxy().
In windoze mode, this will be RedrawWindow(), while in Linux this completely depends on what library you are using to interface to the X server.