Unix Network Programming Manual
Prev Page Next Page

Timers

In many situations in network programming Essential to the use of signals and asynchronous I/O, as well as to normal protocol management are timers. Operating systems provide utilities that allow a programmer to keep track of time in a number of ways, using the internal computer clocks. By using these clocks, a programmer can arrange to have a signal sent to the process when a particular amount of time has elapsed, or when a particular relative time has been reached. For example, when you do something like a broadcast for servers, the responses are asynchronous - not specifically organized in time. That means that some servers might respond very quickly, while other take longer, either because the host is busy or because they are simply slower. When the broadcast is sent, your program needs to give a reasonable amount of time for the servers to respond, and then not wait any longer. If some server takes 5 minutes to respond, you probably don't want it doing work for you anyway. Therefore, you need to set up a timer that will let you terminate the query-for-server process at a reasonable time.

Another example is receive request that may not return because the other end has terminated or made a protocol error. It is reasonable to limit the wait time by starting a timer and when it expires, halt the receive process and cleanup this particular communication channel.

The following code implements an alarm timer, which is a timer that signals the process after the passage of a certain amount of time. The itimerval structure has two parts. The it_value part indicates how long you want to wait for the first signal. The it_interval part indicates how often you want it to occur after that. In this case, the timer interrupt is to occur once, so the interval is set to zero.

In the following pages, examples of timers will be used in discussing various kinds of asynchronous I/O. However, the following functions are typical of the routines that you might set up in a program to ease the management of functions.
Prev Page Next Page