delorie.com/djgpp/doc/libc/libc_688.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

select

Syntax

 
#include <time.h>
#include <string.h>

int
select(int nfds,
	fd_set *readfds,
	fd_set *writefds,
	fd_set *exceptfds,
	struct timeval *timeout)

Description

This function waits for files to be ready for input or output, or to have exceptional condition pending, or for a timeout.

Each fd_set variable is a bitmap representation of a set of file descriptors, one bit for every descriptor. The following macros shall be used to deal with these sets (in the table below, p is a pointer to an fd_set object and n is a file descriptor):

FD_ZERO(p)

Initialize the set p to all zeros.

FD_SET(n, p)

Set member n in set p.

FD_CLR(n, p)

Clear member n in set p.

FD_ISSET(n, p)

Return the value of member n in set p.

FD_SETSIZE

The maximum number of descriptors supported by the system.

The nfds parameter is the number of bits to be examined in each of the fd_set sets: the function will only check file descriptors 0 through nfds - 1, even if some bits are set for descriptors beyond that.

On input, some of the bits of each one of the fd_set sets for which the function should wait, should be set using the FD_SET macro. select examines only those descriptors whose bits are set.

Any of readfds, writefds, and exceptfds can be a NULL pointer, if the caller is not interested in testing the corresponding conditions.

On output, if select returns a non-negative value, each non-NULL argument of the three sets will be replaced with a subset in which a bit is set for every descriptor that was found to be, respectively, ready for input, ready for output, and pending an exceptional condition. Note that if select returns -1, meaning a failure, the descriptor sets are unchanged, so you should always test the return value before looking at the bits in the returned sets.

The timeout value may be a NULL pointer (no timeout, i.e., wait forever), a pointer to a zero-value structure (poll mode, i.e., test once and exit immediately), or a pointer to a struct timeval variable (timeout: select will repeatedly test all the descriptors until some of them become ready, or the timeout expires).

struct timeval is defined as follows:

 
struct timeval {
  time_t tv_sec;
  long tv_usec;
};

Return Value

On successfull return, select returns the number of files ready, or 0, if the timeout expired. The input sets are replaced with subsets that describe which files are ready for which operations. If select returns 0 (i.e., the timeout has expired), all the non-NULL sets have all their bits reset to zero.

On failure, select returns -1, sets errno to a suitable value, and leaves the descriptor sets unchanged.

Portability

ANSI/ISO C No
POSIX No

Example

 
  struct timeval timeout;
  fd_set read_fds, write_fds;
  int i, select_result;

  timeout.tv_sec = 5;  /* 5-second timeout */
  timeout.tv_usec = 0;

  /* Display status of the 5 files open by default.  */
  for (i = 0; i < 5; i++)
    {

      FD_ZERO (&read_fds);
      FD_SET (i, &read_fds);
      select_result = select (i + 1, &read_fds, 0, 0, &timeout);
      if (select_result == -1)
        {
          fprintf(stderr, "%d: Failure for input", i);
          perror("");
        }
      else
        fprintf(stderr,
                "%d: %s ready for input\n", i,
                select_result ? "" : "NOT");
      FD_ZERO (&write_fds);
      FD_SET (i, &write_fds);
      select_result = select (i + 1, 0, &write_fds, 0, &timeout);
      if (select_result == -1)
        {
          fprintf(stderr, "%d: Failure for output", i);
          perror("");
        }
      else
        fprintf(stderr,
                "%d: %s ready for output\n", i,
                select_result ? "" : "NOT");
    }

Implementation Notes

The following notes describe some details pertinent to the DJGPP implementation of select:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright © 2004     Updated Apr 2004