@ngn you should use select() if you have multiple input/output channels such as socket connections as well as console. Then you read/write in non-blocking mode and do your i/o waiting at select(). The other option is to use threads but then you have to worry about shared state.
select() or equivalent on other platforms.
For character i/o the cbreak mode is often good enough. Then if your process hangs you can still regain control.
@bakul the question is, if select() is already waiting for input from stdin (among other file descriptors), can the application read() 1 byte specifically from stdin?
Hm... ideally k4 should give the lowest priority for command input so that the new fd will have a chance to read.
But often people using select() read from lowest ready fd to highest ready fd, which could explain the behavior. Ideally k4 should use round robin for all non-commands file descriptors and if none are ready it should then read from the command input