Search code examples
clinuxmacosiononblocking

How do you do non-blocking console I/O on Linux in C?


How do you do nonblocking console IO on Linux/OS X in C?


Solution

  • You don't, really. The TTY (console) is a pretty limited device, and you pretty much don't do non-blocking I/O. What you do when you see something that looks like non-blocking I/O, say in a curses/ncurses application, is called raw I/O. In raw I/O, there's no interpretation of the characters, no erase processing etc. Instead, you need to write your own code that checks for data while doing other things.

    In modern C programs, you can simplify this another way, by putting the console I/O into a thread or lightweight process. Then the I/O can go on in the usual blocking fashion, but the data can be inserted into a queue to be processed on another thread.

    Update

    Here's a curses tutorial that covers it more.