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.
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: