I l @ ve RuBoard |
6.1 Synchronous Event DemultiplexingA synchronous event demultiplexer is a function supplied by an OS that waits for specified events to occur on a set of event sources. When one or more of the event sources become active, the function returns to its caller. The caller can then process the events originating from many sources. Synchronous event demultiplexers are often used as the basis of reactive server event loops that examine and react to events from clients in a continuous and orderly way. Most operating systems support one or more synchronous event demultiplexing functions, such as
We focus on select() because it's the most common. Event-driven networked applications can use select() to determine which handles can have I/O operations invoked on them synchronously without blocking the application thread that calls them. The C API for the select() function is outlined below: int select (int width, // Maximum handle plus 1 fd_set *read_fds, // Set of "read" handles fd_set *write_fds, // Set of "write" handles fd_set *except_fds, // Set of "exception" handles struct timeval *timeout);// Time to wait for events An fd_set is a structure representing a set of handles to check for I/O events (a handle set). The three fd_set parameters whose addresses are passed in read_fds , write_fds , and except_fds are examined by select() to see if any of their handles are active for reading, writing, or exceptional conditions (such as out-of- band data ), respectively. The select() function informs its caller about active and inactive handles by modifying its fd_set arguments as outlined below:
Each OS platform supplies a basic set of operations that allows applications to query and manipulate fd_set s. Although these operations are macros on some platforms and functions on others, they have a consistent set of names and behaviors:
The final parameter to select() is a pointer to struct timeval , which contains the following two fields: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ }; The following three types of timeval values can be passed to select() to control its time-out behavior:
|
I l @ ve RuBoard |