select(2) System Calls Manual select(2) select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO, fd_set - / (libc -lc) #include typedef /* ... */ fd_set; int select(int nfds, fd_set *_Nullable restrict readfds, fd_set *_Nullable restrict writefds, fd_set *_Nullable restrict exceptfds, struct timeval *_Nullable restrict timeout); void FD_CLR(int fd, fd_set *set); int FD_ISSET(int fd, const fd_set *set); void FD_SET(int fd, fd_set *set); void FD_ZERO(fd_set *set); int pselect(int nfds, fd_set *_Nullable restrict readfds, fd_set *_Nullable restrict writefds, fd_set *_Nullable restrict exceptfds, const struct timespec *_Nullable restrict timeout, const sigset_t *_Nullable restrict sigmask); glibc ( feature_test_macros(7)): pselect(): _POSIX_C_SOURCE >= 200112L : select() FD_SETSIZE (1024) -- -- . poll(2) epoll(7) . select() "" / ( ). / ( read(2) write(2) ) . fd_set . POSIX fd_set FD_SETSIZE. select() "" ( fd_set) . fd_set NULL . : . select() . : FD_ZERO() ( ) set. . FD_SET() fd set. . FD_CLR() fd set. . FD_ISSET() select() . select() FD_ISSET() . FD_ISSET() fd set . select() : readfds . . select() readfds . writefds . . . select() writefds . exceptfds " ". POLLPRI poll(2). select() exceptfds . nfds 1. ( ). timeout timeout timeval ( ) select() . : o o o . timeout . timeval select() . ( .) timeout NULL select() . pselect() pselect() . select() pselect() : o select() struct timeval ( ) pselect() struct timespec ( ). o select() timeout . pselect() . o select() sigmask pselect() sigmask NULL. sigmask ( sigprocmask(2)) NULL pselect() sigmask "select" . ( sigmask NULL pselect().) timeout pselect() : ready = pselect(nfds, &readfds, &writefds, &exceptfds, timeout, &sigmask); : sigset_t origmask; pthread_sigmask(SIG_SETMASK, &sigmask, &origmask); ready = select(nfds, &readfds, &writefds, &exceptfds, timeout); pthread_sigmask(SIG_SETMASK, &origmask, NULL); pselect() . ( . select() . pselect() pselect() sigmask .) timeout select() : struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ }; pselect() timespec(3). select() timeout . ( POSIX.1 .) timeout struct timeval select()s . timeout select(). select() pselect() ( readfds writefds exceptfds). . -1 errno timeout . EBADF . ( .) . EINTR signal(7). EINVAL nfds RLIMIT_NOFILE ( getrlimit(2)). EINVAL timeout . ENOMEM . UNIX select() EAGAIN ENOMEM . POSIX poll(2) select(). EAGAIN EINTR. POSIX.1-2024. select() POSIX.1-2001 4.4BSD ( 4.2BSD). BSD BSD ( System V). System V BSD . pselect() 2.6.16. POSIX.1g, POSIX.1-2001. glibc ( ). fd_set POSIX.1-2001. fd_set: . fd_set . FD_CLR() FD_SET() fd FD_SETSIZE . POSIX fd . select() pselect() O_NONBLOCK. pselect() ( ) . select() . ( / .) usleep(3) usleep(3) select() nfds timeout NULL . select() poll() select() poll(2) epoll(7): #define POLLIN_SET (EPOLLRDNORM | EPOLLRDBAND | EPOLLIN | EPOLLHUP | EPOLLERR) /* Ready for reading */ #define POLLOUT_SET (EPOLLWRBAND | EPOLLWRNORM | EPOLLOUT | EPOLLERR) /* Ready for writing */ #define POLLEX_SET (EPOLLPRI) /* Exceptional condition */ select() . UNIX select() ( / select() /). ( ) select(). . C nfds. glibc fd_set . . pselect() glibc. pselect6(). glibc. pselect6() timeout. glibc timeout . glibc pselect() timeout POSIX.1-2001. pselect6() sigset_t * : struct { const kernel_sigset_t *ss; /* Pointer to signal set */ size_t ss_len; /* Size (in bytes) of object pointed to by 'ss' */ }; 6 . sigprocmask(2) libc . glibc glibc 2.0 pselect() sigmask. glibc 2.1 glibc 2.2.1 _GNU_SOURCE pselect() . POSIX FD_SETSIZE . glibc fd_set FD_SETSIZE 1024 FD_*() . 1023 poll(2) epoll(7) . fd_set - poll(2) epoll(7). POSIX select() nfds-1. . POSIX EBADF. glibc 2.1 glibc pselect() sigprocmask(2) select(). pselect() . glibc ( ) pselect() . select() " " . . . O_NONBLOCK . select() timeout ( EINTR). POSIX.1. pselect() glibc timeout . #include #include #include #include int main(void) { int retval; fd_set rfds; struct timeval tv; /* Watch stdin (fd 0) to see when it has input. */ FD_ZERO(&rfds); FD_SET(0, &rfds); /* Wait up to five seconds. */ tv.tv_sec = 5; tv.tv_usec = 0; retval = select(1, &rfds, NULL, NULL, &tv); /* Don't rely on the value of tv now! */ if (retval == -1) perror("select()"); else if (retval) printf("Data is available now.\n"); /* FD_ISSET(0, &rfds) will be true. */ else printf("No data within five seconds.\n"); exit(EXIT_SUCCESS); } accept(2), connect(2), poll(2), read(2), recv(2), restart_syscall(2), send(2), sigprocmask(2), write(2), timespec(3), epoll(7), time(7) select_tut(2). 3 . . : . 6.18 8 2026 select(2)