echo();
endwin(); / finish curses /
}
Compile this with cc foo.c -lcurses. Notice how much neater the output is when run under
curses. There's a Nutshell book titled UNIX Curses Explained (which is not at all the book of
programmer swear words most people think it is when they pick it up) that describes curses well. The
curses library only offers character-based screen control func-tions. It's a lower common denominator
than software written using specific bit-mapped graphics windowing libraries, but the curses software
is far more portable.
Finally, there is a non-polling read in which the operating system will send your process a signal each
time it has some input ready.
If a program uses interrupt-driven I/O, when it is not handling input it can be doing other processing in
the main function. This is a very efficient use of resources if input is sporadic and there is much
processing to be done. Interrupt-driven programs are much more complex and difficult to get working,
but the paradigm enables a process to make productive use of time otherwise spent waiting for input.
The use of threads diminishes the need to use interrupt-driven I/O techniques.
Programming Challenge
Write an Interrupt-Driven Input Routine on Your System
Interrupt-driven input is a breeze on MS-DOS. The system provides such spartan services
that it is easy to brush them aside and pluck characters direct from the I/O port. Under
SVr4, you will need to do the following:
- Create a signal handler routine that will be invoked to read a character when
the OS sends a signal that one is ready. The signal to catch is SIGPOLL. - The signal handler should read a character, and also reset itself as the
handler for this signal each time it is invoked. Have it echo the character it
just read, and quit if it was a 'q'. Note: this is just for teaching purposes. In
practice the results are usually undefined if you call any standard library
function from within a signal handler. - Make an ioctl() call to inform the OS that you require a signal to be sent
every time input comes in on the standard input. Look at the manpages for
streamio. You will need a command of I_SETSIG and an argument of
S_RDNORM. - Once the signal handler has been set up, the program can do something else
until input comes in. Have it increment a counter. Print the value of the