man(1) Manual page archive


     SELECT(2)                                               SELECT(2)

     NAME
          select - synchronous input/output multiplexing

     SYNOPSIS
          #include <sys/types.h>

          int select(nfds, readfds, writefds, milli);
          fd_set *readfds, *writefds;

     DESCRIPTION
          Select examines a set of file descriptors to see if they
          will block if read or written.  Readfds points to an object
          of type fd_set, which contains a set of descriptors to be
          checked for reading; writefds similarly for writing.  Only
          descriptors 0 through nfds-1 are considered.  The number of
          ready descriptors is returned, and the fd_set pointed to by
          readfds (writefds) is overwritten with a set of descriptors
          ready to be read (written).  The call waits until at least
          one descriptor is ready, or until milli milliseconds have
          passed.

          Either readfds or writefds may be 0 if no descriptors are
          interesting.

          The fd_set type looks like
               typedef struct {
                    unsigned int fds_bits[FDWORDS];
               } fd_set;
          FDWORDS is sufficient to contain as many file descriptors as
          the system will allow (currently 128).  If there are B bits
          in an unsigned int, file descriptor n is represented by
          1<<((n%B)-1) in word fds_bits[n/B].

          These macros should be used to manipulate the contents of an
          fd_set:

          FD_ZERO(s)      clear all bits in set s
          FD_SET(n, s)    set bit for file descriptor n in set s
          FD_CLR(n, s)    clear bit for file descriptor n in s
          FD_ISSET(n, s)  return a value of 1 if bit for file descrip-
                          tor n is set in s, 0 otherwise

     EXAMPLES
          int p[2];
          fd_set wfs;
          pipe(p);
          do {
               FD_SET(p[1], wfs);
               write(p[1], ".", 1);
               i++;

     SELECT(2)                                               SELECT(2)

          } while(select(p[1]+1, (fd_set*)0, wfs, 0) == 1);
          printf("Pipe capacity = %d\n", i);

     SEE ALSO
          read(2)

     DIAGNOSTICS
          EBADF, EFAULT, EINTR

     BUGS
          Milli is rounded up to the nearest second.
          Select is intended for use with streams; file descriptors
          referring to ordinary files or to non-stream special files
          always appear ready.  This is a lie for some special files.