The terminal is a buffer line. It does not send text to the program until you press Enter . There may be a way to disable terminal line buffering, but I suppose this is beyond your scope.
It stops when you press Enter . However, he does not leave immediately. This is what you want to fix. Get rid of this sleep(1) .
Now your program spam text! You gave select one second timeout, right?
// set the time value to 1 second tv.tv_sec = 1; tv.tv_usec = 0;
The reason the timeout is not saved is because select changes the timeout value. From the man page :
On Linux, select () changes the timeout to display the amount of time it did not sleep; most other implementations do not. (POSIX.1-2001 allows either behavior.) This causes problems both when using Linux code that reads the timeout, it is ported to other operating systems, and when the code is ported to Linux, which reuses struct timeval for several select () s in a loop without reinitialization. Consider the undefined timeout after select () returns.
You will need to initialize the timeval before each call to select, not just once at the beginning of the program.
Joey adams
source share