Unix Network Programming Manual
Asynchronous I/O
In many situations in network programming and in other types of application
programming, the sequence of events cannot be predetermined. For example,
if you were developing a program that could accept inputs from a tablet,
from the keyboard or from a mouse, any one of those could occur at any time,
and so you have to be prepared for all three. In most simple programming
situations, you don't have to worry about this. You just read from the
keyboard when you want input and the program I/O is synchronized, i.e.
predictable.
In the three input problem, you need some method to allow the inputs to
arrive whenever necessary, so if the user moves the mouse or hits the
keyboard, you can get the data and store it until you are ready to do
the processing. Normally, the processing takes place immediately, since
user input may well mean that the user wants attention. For example,
pressing a mouse button or hitting a control-C.
In network programming, this problem might arise because there are several
processes that need to communicate with a program, but not in any particular
order or time period, or to handle the case where a process dies in the
middle of a communication stream and leaves a program waiting for input.
In fact, to produce reliable network programs, this is absolutely essential.
In the following pages, signals, timers and certain I/O functions are
explored in some detail. These are the necessary elements for asynchronous
I/O.