SELECT(2)
π SELECT(2) π
okay bestie, welcome to the System Calls Manual β today we're doing select(2) and honestly? she's the OG group chat moderator of file descriptors. let's get into it π«Ά
β¨ NAME β¨
select β synchronous I/O multiplexing
like, one syscall, watching all your fds at once. main character energy fr.
π LIBRARY π
Standard C Library (libc, -lc)
πΈ SYNOPSIS πΈ
#include <sys/select.h>
int
select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout);
FD_SET(fd, &fdset);
FD_CLR(fd, &fdset);
FD_ISSET(fd, &fdset);
FD_ZERO(&fdset);
π¦ DESCRIPTION π¦
so basically, the select() system call examines the I/O descriptor sets whose addresses you pass in readfds, writefds, and exceptfds, to see if some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending β respectively. and the only exceptional condition detectable is out-of-band data received on a socket. that's it. that's the whole menu. π
the first nfds descriptors are checked in each set; i.e., the descriptors from 0 through nfds-1 in the descriptor sets are examined. on return, select() replaces the given descriptor sets with subsets consisting of those descriptors that are ready for the requested operation. and then it returns the total number of ready descriptors in all the sets. slay.
π the macros (they're giving bit manipulation) π
the descriptor sets are stored as bit fields in arrays of integers. the following macros are provided for manipulating such descriptor sets:
FD_ZERO(&fdset)initializes a descriptor setfdsetto the null set.FD_SET(fd, &fdset)includes a particular descriptorfdinfdset.FD_CLR(fd, &fdset)removesfdfromfdset.FD_ISSET(fd, &fdset)is non-zero iffdis a member offdset, zero otherwise.
β οΈ the behavior of these macros is undefined if a descriptor value is less than zero or greater than or equal to FD_SETSIZE, which is normally at least equal to the maximum number of descriptors supported by the system. so like. don't. π
β° the timeout situation β°
if timeout is not a null pointer, it specifies the maximum interval to wait for the selection to complete. system activity can lengthen the interval by an indeterminate amount β so it's a vibe, not a promise, iykyk.
if timeout is a null pointer, select() blocks indefinitely. yes, forever. she's committed. π«Ά
to effect a poll, the timeout argument should not be a null pointer, but it should point to a zero-valued timeval structure. poll vibes, zero waiting. β¨
and bestie, any of readfds, writefds, and exceptfds may be given as null pointers if no descriptors are of interest. no fds? no problem.
π₯ RETURN VALUES π₯
the select() system call returns the number of ready descriptors that are contained in the descriptor sets, or -1 if an error occurred. if the time limit expires, select() returns 0. and if select() returns with an error β including one due to an interrupted system call β the descriptor sets will be unmodified. she doesn't leave you on read mid-crisis, periodt.
π ERRORS π
an error return from select() indicates:
[EBADF]β one of the descriptor sets specified an invalid descriptor.[EFAULT]β one of the argumentsreadfds,writefds,exceptfds, ortimeoutpoints to an invalid address.[EINTR]β a signal was delivered before the time limit expired and before any of the selected events occurred.[EINVAL]β the specified time limit is invalid. one of its components is negative or too large.[EINVAL]β thenfdsargument was invalid.
π SEE ALSO π
accept(2), connect(2), getdtablesize(2), gettimeofday(2), kqueue(2), poll(2), pselect(2), read(2), recv(2), send(2), write(2), clocks(7)
go read pselect(2) next, she's literally the upgraded version. no cap.
π NOTES π
the default size of FD_SETSIZE is currently 1024. in order to accommodate programs which might potentially use a larger number of open files with select(), it is possible to increase this size by having the program define FD_SETSIZE before the inclusion of any header which includes <sys/types.h>. order matters, bestie. header first and it's over. π
if nfds is greater than the number of open files, select() is not guaranteed to examine the unused file descriptors. but for historical reasons, select() will always examine the first 256 descriptors. she's nostalgic like that.
π STANDARDS π
the select() system call and FD_CLR(), FD_ISSET(), FD_SET(), and FD_ZERO() macros conform with IEEE Std 1003.1-2001 (βPOSIX.1β). certified. iconic. π
π§ HISTORY π§
the select() system call appeared in 4.2BSD. ancient. legendary. still working the room. π
π BUGS π
version 2 of the Single UNIX Specification (βSUSv2β) allows systems to modify the original timeout in place. thus, it is unwise to assume that thetimeoutvalue will be unmodified by theselect()system call.
FreeBSD does not modify the return value, which can cause problems for applications ported from other systems. so if your port is acting weird, it's not you, it's the platform. π
and that's the whole page, bestie!! this one came out of the Debian camp, dated June 25, 2020 β so go forth and multiplex responsibly. mwah. πβ¨ππ¦πΈπ«Ά
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms