#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
int main ()
{
char name[20];
fd_set input_set;
struct timeval timeout;
int ready_for_reading = 0;
int read_bytes = 0;
FD_ZERO(&input_set );
FD_SET(0, &input_set);
timeout.tv_sec = 10;
timeout.tv_usec = 0;
printf("Enter Username: (in 15 seconds)\n");
printf("Time start now!!!\n");
ready_for_reading = select(1, &input_set, NULL, NULL, &timeout);
if (ready_for_reading == -1) {
printf("Unable to read your input\n");
return -1;
} else {
if (ready_for_reading) {
read_bytes = read(0, name, 19);
printf("Read, %d bytes from input : %s \n", read_bytes, name);
} else {
printf(" 10 Seconds are over - no data input \n");
}
}
return 0;
}
How to do the same, but not only once, but in an infinite loop that breaks after collision with the string "quit" (for example). Every time I tried - unsuccessfully. Therefore, if data has not been entered after 10 seconds, the program simply prints “10 seconds - no data entry”, and then starts to wait again. The same thing after input - it just starts again and behaves the same every time in an infinite loop.
Already a little desperate, please help.
Thank.
source
share