Nonblocking Input

Written by Ian on Friday 27/04/07


Did you think non-blocking input on the linux terminal was impossible without ncurses? Well someone has found a way. You can have a kbit() or ifget() function in linux now.


The main input to a console program comes from standard in, stdin. This is provided, by default, to any console program on file descriptor 0 (standard out is fd 1 and standard error is fd 2).


So in a nutshell, this method acts directly on the file descriptor, and passes it into the select command which looks for activity on that file descriptor.
It's a bit dodgy but it works.


To really make things work properly though, it's necessary to tell the shell input to be non-canonical (which means to do file I/O immediately without waiting to bunch up characters - which is the far more efficient default action).


Anyway, you can download the example 'C' code from here


This was originally modified from an excellent posting on the cc.byexamples.com website:

http://cc.byexamples.com/20070408/non-blocking-user-input-in-loop-without-ncurses/