select.2 (2012 03)
s
select(2) select(2)
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>
.
#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);
WARNINGS
The file descriptor masks are always modified on return, even if the call returns as the result of a
timeout.
DEPENDENCIES
select() supports the following devices and file types:
• pipes
• fifo special files (named pipes)
• all serial devices
• All ITEs (internal terminal emulators) and HP-HIL input devices
• lan(7) special files
• pty (7) special files
• sockets
HP-UX 11i Version 3: March 2012 − 5 − Hewlett-Packard Company 5