Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 14.4 I/O Multiplexing 505


The reason we have to add 1 to the maximum descriptor number is that descriptors
start at 0, and the first argument is really a count of the number of descriptors to check
(starting with descriptor 0).

readset: 1 0 0 1

writeset: 0 1 1 0

fd 0 fd 1 fd 2 fd 3

none of these bits arelooked at

maxfdp1=4

Figure 14.16Example descriptor sets forselect

Thereare three possible return values fromselect.


  1. A return value of−1means that an error occurred. This can happen, for
    example, if a signal is caught beforeany of the specified descriptors areready.
    In this case, none of the descriptor sets will be modified.

  2. Areturn value of 0 means that no descriptors areready.This happens if the
    time limit expires beforeany of the descriptors areready.When this happens,
    all the descriptor sets will be zeroed out.

  3. A positive return value specifies the number of descriptors that areready.This
    value is the sum of the descriptors ready in all three sets, so if the same
    descriptor is ready to be readandwritten, it will be counted twice in the return
    value. The only bits left on in the three descriptor sets arethe bits
    corresponding to the descriptors that areready.


We now need to be morespecific about what ‘‘ready’’means.

•Adescriptor in the read set (readfds) is considered ready if areadfrom that
descriptor won’t block.
•Adescriptor in the write set (writefds) is considered ready if awriteto that
descriptor won’t block.
•Adescriptor in the exception set (exceptfds) is considered ready if an exception
condition is pending on that descriptor.Currently, an exception condition
corresponds to either the arrival of out-of-band data on a network connection or
certain conditions occurring on a pseudo terminal that has been placed into
packet mode. (Section 15.10 of Stevens[ 1990 ]describes this latter condition.)
•File descriptors for regular files always return ready for reading, writing, and
exception conditions.
Free download pdf