Seth Woolley's Man Viewer

Manual for select - man 2 select

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

SELECT(2)                  Linux Programmer's Manual                 SELECT(2)

       select(2,7,2 select_tut),  pselect,  FD_CLR,  FD_ISSET, FD_SET, FD_ZERO - synchronous I/O

       /* According to POSIX 1003.1-2001 */
       #include <sys/select.h>

       /* According to earlier standards */
       #include <sys/time.h>
       #include <sys/types.h>
       #include <unistd.h>

       int select(2,7,2 select_tut)(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
       struct timeval *timeout(1,3x,3x cbreak));

       int   pselect(int   n,   fd_set   *readfds,  fd_set  *writefds,  fd_set
       *exceptfds, const struct timespec *timeout(1,3x,3x cbreak), const sigset_t *sigmask);

       FD_CLR(int fd, fd_set *set(7,n,1 builtins));
       FD_ISSET(int fd, fd_set *set(7,n,1 builtins));
       FD_SET(int fd, fd_set *set(7,n,1 builtins));
       FD_ZERO(fd_set *set(7,n,1 builtins));

       The functions select(2,7,2 select_tut) and pselect wait for a number of file(1,n)  descriptors
       to change status.

       Their function is identical, with three differences:

       (i)    The  select(2,7,2 select_tut)  function  uses  a  timeout(1,3x,3x cbreak) that is a struct timeval
              (with seconds and microseconds), while  pselect  uses  a  struct
              timespec (with seconds and nanoseconds).

       (ii)   The select(2,7,2 select_tut) function may update(7,n) the timeout(1,3x,3x cbreak) parameter to indicate
              how much time(1,2,n) was left. The pselect  function  does  not  change
              this parameter.

       (iii)  The  select(2,7,2 select_tut)  function  has  no sigmask parameter, and behaves as
              pselect called with NULL sigmask.

       Three independent sets of descriptors are  watched.   Those  listed  in(1,8)
       readfds will be watched to see if(3,n) characters become available for read-
       ing (more precisely, to see if(3,n) a read(2,n,1 builtins) will not block - in(1,8) particular, a
       file(1,n)  descriptor  is also ready on end-of-file), those in(1,8) writefds will
       be watched to see if(3,n) a write(1,2) will not block,  and  those  in(1,8)  exceptfds
       will  be  watched  for  exceptions.   On exit(3,n,1 builtins), the sets are modified in(1,8)
       place to indicate which descriptors actually changed status.

       Four macros are provided to manipulate the sets.  FD_ZERO will clear(1,3x,3x clrtobot)  a
       set.   FD_SET  and  FD_CLR add or remove a given descriptor from a set.
       FD_ISSET tests to see if(3,n) a descriptor is part of the set(7,n,1 builtins); this is  use-
       ful after select(2,7,2 select_tut) returns.

       n  is the highest-numbered descriptor in(1,8) any of the three sets, plus 1.

       timeout(1,3x,3x cbreak) is an upper bound on the amount of time(1,2,n) elapsed  before  select(2,7,2 select_tut)
       returns. It may be zero, causing select(2,7,2 select_tut) to return immediately. (This is
       useful for polling.) If timeout(1,3x,3x cbreak) is NULL (no timeout(1,3x,3x cbreak)), select(2,7,2 select_tut) can  block

       sigmask  is  a  pointer to a signal(2,7) mask (see sigprocmask(2)); if(3,n) it is
       not NULL, then pselect first replaces the current signal(2,7)  mask  by  the
       one  pointed  to  by sigmask, then does the `select(2,7,2 select_tut)' function, and then
       restores the original signal(2,7) mask again.

       The idea of pselect is that if(3,n) one wants to wait for an event, either a
       signal(2,7)  or  something on a file(1,n) descriptor, an atomic test is needed to
       prevent race conditions. (Suppose the signal(2,7) handler sets a global flag
       and  returns.  Then  a  test  of this global flag followed by a call of
       select(2,7,2 select_tut)() could hang indefinitely if(3,n) the signal(2,7) arrived just  after  the
       test but just before the call. On the other hand, pselect allows one to
       first block signals, handle the signals that have come  in(1,8),  then  call
       pselect()  with  the  desired sigmask, avoiding the race.)  Since Linux
       today does not have a pselect() system call, the current glibc2 routine
       still contains this race.

   The timeout(1,3x,3x cbreak)
       The time(1,2,n) structures involved are defined in(1,8) <sys/time.h> and look(1,8,3 Search::Dict) like

              struct timeval {
                  long    tv_sec;         /* seconds */
                  long    tv_usec;        /* microseconds */


              struct timespec {
                  long    tv_sec;         /* seconds */
                  long    tv_nsec;        /* nanoseconds */

       (However, see below on the POSIX 1003.1-2001 versions.)

       Some  code  calls  select(2,7,2 select_tut) with all three sets empty, n zero, and a non-
       null timeout(1,3x,3x cbreak) as a fairly portable way to sleep(1,3)  with  subsecond  preci-

       On Linux, the function select(2,7,2 select_tut) modifies timeout(1,3x,3x cbreak) to reflect the amount of
       time(1,2,n) not slept; most other implementations do not do this.  This causes
       problems  both  when  Linux code which reads timeout(1,3x,3x cbreak) is ported to other
       operating systems, and when code is  ported  to  Linux  that  reuses  a
       struct  timeval  for  multiple select(2,7,2 select_tut)s in(1,8) a loop without reinitializing
       it.  Consider timeout(1,3x,3x cbreak) to be undefined after select(2,7,2 select_tut) returns.

       On success, select(2,7,2 select_tut) and pselect return the number  of  descriptors  con-
       tained in(1,8) the three returned descriptor sets (that is, the total number
       of one bits in(1,8) readfds, writefds, exceptfds) which may be zero  if(3,n)  the
       timeout(1,3x,3x cbreak)  expires  before anything interesting happens.  On error(8,n), -1 is
       returned, and errno is set(7,n,1 builtins) appropriately; the sets and  timeout(1,3x,3x cbreak)  become
       undefined, so do not rely on their contents after an error.

       EBADF  An invalid file(1,n) descriptor was given in(1,8) one of the sets.

       EINTR  A non blocked signal(2,7) was caught.

       EINVAL n  is negative or the value contained within timeout(1,3x,3x cbreak) is invalid.

       ENOMEM select(2,7,2 select_tut) was unable to allocate memory for internal tables.

       #include <stdio.h>
       #include <sys/time.h>
       #include <sys/types.h>
       #include <unistd.h>

       main(void) {
           fd_set rfds;
           struct timeval tv;
           int retval;

           /* Watch stdin (fd 0) to see when it has input. */
           FD_SET(0, &rfds);
           /* Wait up to five seconds. */
           tv.tv_sec = 5;
           tv.tv_usec = 0;

           retval = select(2,7,2 select_tut)(1, &rfds, NULL, NULL, &tv);
           /* Don't rely on the value of tv now! */

           if(3,n) (retval == -1)
               perror(1,3)("select(2,7,2 select_tut)()");
           else if(3,n) (retval)
               printf(1,3,1 builtins)("Data is available now.\n");
               /* FD_ISSET(0, &rfds) will be true. */
               printf(1,3,1 builtins)("No data within five seconds.\n");

           return 0;

       4.4BSD (the select(2,7,2 select_tut) function first appeared in(1,8) 4.2BSD).  Generally  por-
       table to/from non-BSD systems supporting clones of the BSD socket(2,7,n) layer
       (including System V variants).  However, note that the System V variant
       typically  sets  the  timeout(1,3x,3x cbreak) variable before exit(3,n,1 builtins), but the BSD variant
       does not.

       The pselect function is defined in(1,8) IEEE  Std  1003.1g-2000  (POSIX.1g),
       and  part  of  POSIX  1003.1-2001.   It is found in(1,8) glibc2.1 and later.
       Glibc2.0 has a function with this name, that however does  not  take  a
       sigmask parameter.

       An  fd_set  is  a  fixed size buffer. Executing FD_CLR or FD_SET with a
       value of fd that is negative or is equal to or larger  than  FD_SETSIZE
       will  result in(1,8) undefined behavior. Moreover, POSIX requires fd to be a
       valid file(1,n) descriptor.

       Concerning the types involved, the classical situation is that the  two
       fields  of  a struct timeval are longs (as shown above), and the struct
       is defined in(1,8) <sys/time.h>.  The POSIX 1003.1-2001 situation is

              struct timeval {
                  time_t         tv_sec;     /* seconds */
                  suseconds_t    tv_usec;    /* microseconds */

       where the struct is defined in(1,8) <sys/select.h> and the data types time_t
       and suseconds_t are defined in(1,8) <sys/types.h>.

       Concerning  prototypes,  the  classical  situation  is  that one should
       include <time.h> for select(2,7,2 select_tut).  The POSIX 1003.1-2001 situation  is  that
       one  should  include  <sys/select.h> for select(2,7,2 select_tut) and pselect.  Libc4 and
       libc5 do not have a <sys/select.h> header; under glibc  2.0  and  later
       this header exists.  Under glibc 2.0 it unconditionally gives the wrong
       prototype for pselect, under glibc  2.1-2.2.1  it  gives  pselect  when
       _GNU_SOURCE  is  defined,  under  glibc  2.2.2-2.2.4  it  gives it when
       _XOPEN_SOURCE is defined and has a value of 600 or larger.   No  doubt,
       since POSIX 1003.1-2001, it should give the prototype by default.

       pselect is currently emulated with a user-space wrapper that has a race
       condition.  For reliable (and more portable) signal(2,7) trapping,  use  the
       self-pipe  trick.  (Where a signal(2,7) handler writes to a pipe(2,8) whose other
       end is read(2,n,1 builtins) by the main loop.)

       Under Linux, select(2,7,2 select_tut) may report a socket(2,7,n) file(1,n) descriptor as  "ready  for
       reading",  while  nevertheless a subsequent read(2,n,1 builtins) blocks. This could for
       example happen when data has arrived but  upon  examination  has  wrong
       checksum  and  is discarded. There may be other circumstances.  Thus it
       may be safer to use O_NONBLOCK on sockets that should not block.

       For a tutorial with discussion and examples, see select_tut(2).

       For vaguely related stuff, see accept(2,8)(2), connect(2), poll(2), read(2,n,1 builtins)(2),
       recv(2), send(2,n)(2), sigprocmask(2), write(1,2)(2)

Linux 2.4                         2001-02-09                         SELECT(2)

References for this manual (incoming links)