xref: /original-bsd/lib/libc/sys/select.2 (revision babae2df)
1.\" Copyright (c) 1983, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)select.2	8.2 (Berkeley) 03/25/94
7.\"
8.Dd
9.Dt SELECT 2
10.Os BSD 4.2
11.Sh NAME
12.Nm select
13.Nd synchronous I/O multiplexing
14.Sh SYNOPSIS
15.Fd #include <sys/types.h>
16.Fd #include <sys/time.h>
17.Fd #include <unistd.h>
18.Ft int
19.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
20.Fn FD_SET fd &fdset
21.Fn FD_CLR fd &fdset
22.Fn FD_ISSET fd &fdset
23.Fn FD_ZERO &fdset
24.Sh DESCRIPTION
25.Fn Select
26examines the I/O descriptor sets whose addresses are passed in
27.Fa readfds ,
28.Fa writefds ,
29and
30.Fa exceptfds
31to see if some of their descriptors
32are ready for reading, are ready for writing, or have an exceptional
33condition pending, respectively.
34The first
35.Fa nfds
36descriptors are checked in each set;
37i.e., the descriptors from 0 through
38.Fa nfds Ns No -1
39in the descriptor sets are examined.
40On return,
41.Fn select
42replaces the given descriptor sets
43with subsets consisting of those descriptors that are ready
44for the requested operation.
45.Fn Select
46returns the total number of ready descriptors in all the sets.
47.Pp
48The descriptor sets are stored as bit fields in arrays of integers.
49The following macros are provided for manipulating such descriptor sets:
50.Fn FD_ZERO &fdsetx
51initializes a descriptor set
52.Fa fdset
53to the null set.
54.Fn FD_SET fd &fdset
55includes a particular descriptor
56.Fa fd
57in
58.Fa fdset .
59.Fn FD_CLR fd &fdset
60removes
61.Fa fd
62from
63.Fa fdset .
64.Fn FD_ISSET fd &fdset
65is non-zero if
66.Fa fd
67is a member of
68.Fa fdset ,
69zero otherwise.
70The behavior of these macros is undefined if
71a descriptor value is less than zero or greater than or equal to
72.Dv FD_SETSIZE ,
73which is normally at least equal
74to the maximum number of descriptors supported by the system.
75.Pp
76If
77.Fa timeout
78is a non-nil pointer, it specifies a maximum interval to wait for the
79selection to complete.  If
80.Fa timeout
81is a nil pointer, the select blocks indefinitely.  To affect a poll, the
82.Fa timeout
83argument should be non-nil, pointing to a zero-valued timeval structure.
84.Pp
85Any of
86.Fa readfds ,
87.Fa writefds ,
88and
89.Fa exceptfds
90may be given as nil pointers if no descriptors are of interest.
91.Sh RETURN VALUES
92.Fn Select
93returns the number of ready descriptors that are contained in
94the descriptor sets,
95or -1 if an error occurred.
96If the time limit expires,
97.Fn select
98returns 0.
99If
100.Fn select
101returns with an error,
102including one due to an interrupted call,
103the descriptor sets will be unmodified.
104.Sh ERRORS
105An error return from
106.Fn select
107indicates:
108.Bl -tag -width Er
109.It Bq Er EBADF
110One of the descriptor sets specified an invalid descriptor.
111.It Bq Er EINTR
112A signal was delivered before the time limit expired and
113before any of the selected events occurred.
114.It Bq Er EINVAL
115The specified time limit is invalid.  One of its components is
116negative or too large.
117.El
118.Sh SEE ALSO
119.Xr accept 2 ,
120.Xr connect 2 ,
121.Xr getdtablesize 2 ,
122.Xr gettimeofday 2 ,
123.Xr read 2 ,
124.Xr recv 2 ,
125.Xr send 2 ,
126.Xr write 2
127.Sh BUGS
128Although the provision of
129.Xr getdtablesize 2
130was intended to allow user programs to be written independent
131of the kernel limit on the number of open files, the dimension
132of a sufficiently large bit field for select remains a problem.
133The default size
134.Dv FD_SETSIZE
135(currently 256) is somewhat larger than
136the current kernel limit to the number of open files.
137However, in order to accommodate programs which might potentially
138use a larger number of open files with select, it is possible
139to increase this size within a program by providing
140a larger definition of
141.Dv FD_SETSIZE
142before the inclusion of
143.Aq Pa sys/types.h .
144.Pp
145.Fn Select
146should probably return the time remaining from the original timeout,
147if any, by modifying the time value in place.
148This may be implemented in future versions of the system.
149Thus, it is unwise to assume that the timeout value will be unmodified
150by the
151.Fn select
152call.
153.Sh HISTORY
154The
155.Nm
156function call appeared in
157.Bx 4.2 .
158