HPUX select[2]






 select(2)                                                         select(2)





 NAME
      select - synchronous I/O multiplexing

 SYNOPSIS
      #include <time.h>

      int select(
           size_t nfds,
           int *readfds,
           int *writefds,
           int *exceptfds,
           const struct timeval *timeout
      );

 DESCRIPTION
      select() examines the file descriptors specified by the bit masks
      readfds, writefds, and exceptfds.  The bits from 0 through nfds-1 are
      examined.  File descriptor f is represented by the bit 1<<f in the
      masks.  More formally, a file descriptor is represented by:

           fds[(f / BITS_PER_INT)] & (1 << (f % BITS_PER_INT))

      When select() completes successfully it returns the three bit masks
      modified as follows: For each file descriptor less than nfds, the
      corresponding bit in each mask is set if the bit was set upon entry
      and the file descriptor is ready for reading or writing, or has an
      exceptional condition pending.

      If timeout is a non-zero pointer, it specifies a maximum interval to
      wait for the selection to complete.  If timeout is a zero pointer, the
      select waits until an event causes one of the masks to be returned
      with a valid (non-zero) value.  To poll, the timeout argument should
      be non-zero, pointing to a zero valued timeval structure.  Specific
      implementations may place limitations on the maximum timeout interval
      supported.  The constant MAX_ALARM defined in <sys/param.h> specifies
      the implementation-specific maximum (in seconds).  Whenever timeout
      specifies a value greater than this maximum, it is silently rounded
      down to this maximum.  On all implementations, MAX_ALARM is guaranteed
      to be at least 31 days (in seconds).  Note that the use of a timeout
      does not affect any pending timers set up by alarm() or setitimer()
      (see alarm(2) or setitimer(2)).

      Any or all of readfds, writefds, and exceptfds can be given as 0 if no
      descriptors are of interest.  If all the masks are given as 0 and
      timeout is not a zero pointer, select() blocks for the time specified,
      or until interrupted by a signal.  If all the masks are given as 0 and
      timeout is a zero pointer, select() blocks until interrupted by a
      signal.

      Ordinary files always select true whenever selecting on reads, writes,
      and/or exceptions.



 Hewlett-Packard Company            - 1 -     HP-UX Release 9.0: August 1992






 select(2)                                                         select(2)





 EXAMPLES
      The following call to select() checks if any of 4 terminals are ready
      for reading.  select() times out after 5 seconds if no terminals are
      ready for reading.  Note that the code for opening the terminals or
      reading from the terminals is not shown in this example.  Also, note
      that this example must be modified if the calling process has more
      than 32 file descriptors open.  Following this first example is an
      example of select with more than 32 file descriptors.

           #define MASK(f)     (1 << (f))
           #define NTTYS 4

           int tty[NTTYS];
           int ttymask[NTTYS];
           int readmask = 0;
           int readfds;
           int nfound, i;
           struct timeval timeout;

              /* First open each terminal for reading and put the
               * file descriptors into array tty[NTTYS].  The code
               * for opening the terminals is not shown here.
               */

           for (i=0; i < NTTYS; i++) {
              ttymask[i] = MASK(tty[i]);
              readmask |= ttymask[i];
           }

           timeout.tv_sec  = 5;
           timeout.tv_usec = 0;
           readfds = readmask;

           /* select on NTTYS+3 file descriptors if stdin, stdout
            * and stderr are also open
            */
           if ((nfound = select (NTTYS+3, &readfds, 0, 0, &timeout)) == -1)
              perror ("select failed");
           else if (nfound == 0)
              printf ("select timed out \n");
           else for (i=0; i < NTTYS; i++)
              if (ttymask[i] & readfds)
                 /* Read from tty[i].  The code for reading
                  * is not shown here.
                  */
              else printf ("tty[%d] is not ready for reading \n",i);

      The following example is the same as the previous example, except that
      it works for more than 32 open files.  Definitions for howmany,
      fd_set, and NFDBITS are in <sys/types.h>.




 Hewlett-Packard Company            - 2 -     HP-UX Release 9.0: August 1992






 select(2)                                                         select(2)





           #include <sys/param.h>
           #include <sys/types.h>
           #include <sys/time.h>

           #define MASK(f)     (1 << (f))
           #define NTTYS NOFILE - 3
           #define NWORDS  howmany(FD_SETSIZE, NFDBITS)

           int tty[NTTYS];
           int ttymask[NTTYS];
           struct fd_set readmask, readfds;
           int nfound, i, j, k;
           struct timeval timeout;

              /* First open each terminal for reading and put the
               * file descriptors into array tty[NTTYS].  The code
               * for opening the terminals is not shown here.
               */

              for (k=0; k < NWORDS; k++)
                 readmask.fds_bits[k] = 0;

              for (i=0, k=0; i < NTTYS && k < NWORDS; k++)
                 for (j=0; j < NFDBITS && i < NTTYS; j++, i++) {
                    ttymask[i] = MASK(tty[i]);
                    readmask.fds_bits[k] |= ttymask[i];
                 }

              timeout.tv_sec  = 5;
              timeout.tv_usec = 0;
              for (k=0; k < NWORDS; k++)
                 readfds.fds_bits[k] = readmask.fds_bits[k];

              /* select on NTTYS+3 file descriptors if stdin, stdout
               * and stderr are also open
               */
              if ((nfound = select (NTTYS+3, &readfds, 0, 0, &timeout)) == -1)
                 perror ("select failed");
              else if (nfound == 0)
                 printf ("select timed out \n");
              else for (i=0, k=0; i < NTTYS && k < NWORDS; k++)
                 for (j=0; j < NFDBITS && i < NTTYS; j++, i++)
                    if (ttymask[i] & readfds.fds_bits[k])
                       /* Read from tty[i].  The code for reading
                        * is not shown here.
                        */
                    else printf ("tty[%d] is not ready for reading \n",i);

 RETURN VALUE
      select() returns the number of descriptors contained in the bit masks,
      or -1 if an error occurred.  If the time limit expires, select()



 Hewlett-Packard Company            - 3 -     HP-UX Release 9.0: August 1992






 select(2)                                                         select(2)





      returns 0 and all the masks are cleared.

 ERRORS
      select() fails if any of the following conditions are encountered:

           [EBADF]        One or more of the bit masks specified an invalid
                          descriptor.

           [EINTR]        A signal was delivered before any of the selected
                          for events occurred or before the time limit
                          expired.

           [EFAULT]       One or more of the pointers was invalid.  The
                          reliable detection of this error is implementation
                          dependent.

           [EINVAL]       Invalid timeval passed for timeout.

           [EINVAL]       The value of nfds is less than zero.

 WARNINGS
      Check all references to signal(5) for appropriateness on systems that
      support sigvector(2).  sigvector(2) can affect the behavior described
      on this page.

      The file descriptor masks are always modified on return, even if the
      call returns as the result of a timeout.

 DEPENDENCIES
      Series 300/400
           select() supports the following devices and file types:

                o  pipes
                o  fifo special files (named pipes)
                o  All serial interfaces
                o  All ITEs (internal terminal emulators) and HP-HIL input
                   devices
                o  pty(7) special files
                o  sockets
                o  HP 98643 LAN interface card driver

           File types not supporting select() always return true.

      Series 700/800
           select() supports the following devices and file types:

                o  pipes
                o  fifo special files (named pipes)
                o  all serial devices
                o  All ITEs (internal terminal emulators) and HP-HIL input
                   devices



 Hewlett-Packard Company            - 4 -     HP-UX Release 9.0: August 1992






 select(2)                                                         select(2)





                o  hpib(7) special files
                o  gpio(7) special files (Series 800 Only for Release 8.0)
                o  lan(7) special files
                o  pty(7) special files
                o  sockets

           The convention for device files that do not support select() is
           to always return true for those conditions the user is selecting
           on.

           Consult individual device manual entries to determine the extent
           to which any particular driver supports select.

      HP Clustered Environment
           In a clustered environment, select() is not supported for
           distributed fifos; i.e., fifos that are open simultaneously on
           multiple machines.  In this case an error of EINVAL is returned.

 AUTHOR
      select() was developed by HP and the University of California,
      Berkeley.

 SEE ALSO
      fcntl(2), read(2), write(2).






























 Hewlett-Packard Company            - 5 -     HP-UX Release 9.0: August 1992