linux
select
C LANGUAGE
Linux man page:
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous
I/O multiplexing
โโโโ #include <sys/select.h>
โโโโ int select(int nfds, fd_set *restrict readfds,
โโโโ fd_set *restrict writefds, fd_set *restrict exceptfds,
โโโโ struct timeval *restrict timeout);
โโโโ void FD_CLR(int fd, fd_set *set);
โโโโ int FD_ISSET(int fd, fd_set *set);
โโโโ void FD_SET(int fd, fd_set *set);
โโโโ void FD_ZERO(fd_set *set);
โโโโ int pselect(int nfds, fd_set *restrict readfds,
โโโโ fd_set *restrict writefds, fd_set *restrict exceptfds,
โโโโ const struct timespec *restrict timeout,
โโโโ const sigset_t *restrict sigmask);
Feature Test Macro Requirements for glibc (see
feature_test_macros(7)):
โโโโ pselect():
โโโโ _POSIX_C_SOURCE >= 200112L
DESCRIPTION top WARNING: select() can monitor only file descriptors numbers that
are less than FD_SETSIZE (1024)โan unreasonably low limit for
many modern applicationsโand this limitation will not change.
All modern applications should instead use poll(2) or epoll(7),
which do not suffer this limitation.
select() allows a program to monitor multiple file descriptors,
waiting until one or more of the file descriptors become "ready"
for some class of I/O operation (e.g., input possible). A file
descriptor is considered ready if it is possible to perform a
corresponding I/O operation (e.g., read(2), or a sufficiently
small write(2)) without blocking.
File descriptor sets
The principal arguments of select() are three "sets" of file
descriptors (declared with the type fd_set), which allow the
caller to wait for three classes of events on the specified set
of file descriptors. Each of the fd_set arguments may be
specified as NULL if no file descriptors are to be watched for
the corresponding class of events.
Note well: Upon return, each of the file descriptor sets is
modified in place to indicate which file descriptors are
currently "ready". Thus, if using select() within a loop, the
sets must be reinitialized before each call.
The contents of a file descriptor set can be manipulated using
the following macros:
FD_ZERO()
This macro clears (removes all file descriptors from) set.
It should be employed as the first step in initializing a
file descriptor set.
FD_SET()
This macro adds the file descriptor fd to set. Adding a
file descriptor that is already present in the set is a
no-op, and does not produce an error.
FD_CLR()
This macro removes the file descriptor fd from set.
Removing a file descriptor that is not present in the set
is a no-op, and does not produce an error.
FD_ISSET()
select() modifies the contents of the sets according to
the rules described below. After calling select(), the
FD_ISSET() macro can be used to test if a file descriptor
is still present in a set. FD_ISSET() returns nonzero if
the file descriptor fd is present in set, and zero if it
is not.
Arguments
The arguments of select() are as follows:
readfds
The file descriptors in this set are watched to see if
they are ready for reading.
A file descriptor is ready for reading if a read operation will not block; in particular, a file descriptor is also ready on end-of-file.
After select() has returned, readfds will be cleared of
all file descriptors except for those that are ready for
reading.
writefds
The file descriptors in this set are watched to see if they are ready for writing.
A file descriptor is ready for writing if a write operation will not block.
However,
even if a file descriptor indicates as writable, a large
write may still block.
After select() has returned, writefds will be cleared of
all file descriptors except for those that are ready for
writing.
exceptfds
The file descriptors in this set are watched for "exceptional conditions".
For examples of some exceptional conditions, see the discussion of POLLPRI in poll(2). After select() has returned, exceptfds will be cleared of
all file descriptors except for those for which an exceptional condition has occurred.
nfds
This argument should be set to the highest-numbered file descriptor in any of the three sets, plus 1.
The
indicated file descriptors in each set are checked, up to this limit (but see BUGS).
timeout
The timeout argument is a timeval structure (shown below)
that specifies the interval that select() should block
waiting for a file descriptor to become ready. The call
will block until either:
โโโโ โข a file descriptor becomes ready;
โโโโ โข the call is interrupted by a signal handler; or
โโโโ โข the timeout expires.
โโโโ Note that the timeout interval will be rounded up to the
โโโโ system clock granularity, and kernel scheduling delays
โโโโ mean that the blocking interval may overrun by a small
โโโโ amount.
โโโโ If both fields of the timeval structure are zero, then
โโโโ select() returns immediately. (This is useful for
โโโโ polling.)
โโโโ If timeout is specified as NULL, select() blocks
โโโโ indefinitely waiting for a file descriptor to become
โโโโ ready.
RETURN VALUE
On success, select() and pselect() return the number of file
descriptors contained in the three returned descriptor sets (that
is, the total number of bits that are set in readfds, writefds,
exceptfds). The return value may be zero if the timeout expired
before any file descriptors became ready.
โโโโ On error, -1 is returned, and errno is set to indicate the error;
โโโโ the file descriptor sets are unmodified, and timeout becomes
โโโโ undefined.
TCP Client/Server (select) ้ไฝ่งๅฟต
Linux 2.6:
How to use epoll? A complete example in C, by Mukund Sivaraman
่ฆๅจ linux 2.6 ไปฅไธ, ไธ่ฝ็จcygwin
Red-Black Trees:
HOME WORKS: