Try โ€‚โ€‰HackMD

select system call-synchronous I/O multiplexing

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


Data Structure Visualizations

HOME WORKS:

  1. ็ทด็ฟ’ epoll ่ˆ‡ non-blocking I/O
  2. ่จญ่จˆ 1 client ๆœ‰ nprintf function ๅฏไปฅ่ผธๅ‡บ่‡ณๆŒ‡ๅฎš็š„ server (HINT: sprintf)
    ssize_t nprintf (int sockfd, const char *format, โ€ฆ);

  1. ่ค‡็ฟ’ C ไนŸๅฏไปฅๅฏซ็š„ๅพˆOOP ็ฐกๅ–ฎ็ฏ„ไพ‹่ˆ‡็ฐกๆ˜“Makefile
    ๆ‚จๆ˜ฏๅฆ ่ƒฝๅฐ‡ 2 ๆ”นๅฏซ, ๆ–นไพฟๆ—ฅๅพŒไฝฟ็”จ?