# select system call-synchronous I/O multiplexing ![](https://i.imgur.com/GH6QIvk.jpg) ###### 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. ```c= 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. :::info 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. :::info 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** :::info 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** :::info 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) 運作觀念](https://hackmd.io/@pingulinux/select/) Linux 2.6: [How to use epoll? A complete example in C, by Mukund Sivaraman ](https://blog.csdn.net/daniel_ustc/article/details/8757570) **要在 linux 2.6 以上, 不能用cygwin** Red-Black Trees: [ Data Structure Visualizations](https://www.cs.usfca.edu/~galles/visualization/Algorithms.html) **HOME WORKS:** 1. 練習 epoll 與 non-blocking I/O 2. 設計 1 client 有 nprintf function 可以輸出至指定的 server (HINT: sprintf) ssize_t nprintf (int sockfd, const char *format, ...); ![](https://i.imgur.com/wQ2Cs4L.jpg) 3. 複習 [C 也可以寫的很OOP 簡單範例與簡易Makefile](https://hackmd.io/@pingulinux/c-oop) 您是否 能將 2 改寫, 方便日後使用?