PSELECT(3P)
๐ PROLOG ๐
okay so real talk before we get into it โ this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so bestie, go check the corresponding Linux manual page for the tea on Linux behavior), or honestly the interface may not even be implemented on Linux at all. ๐
๐ธ NAME ๐ธ
pselect, select โ synchronous I/O multiplexing โจ
๐ SYNOPSIS ๐
#include <sys/select.h>
int pselect(int nfds, fd_set *restrict readfds,
fd_set *restrict writefds, fd_set *restrict errorfds,
const struct timespec *restrict timeout,
const sigset_t *restrict sigmask);
int select(int nfds, fd_set *restrict readfds,
fd_set *restrict writefds, fd_set *restrict errorfds,
struct timeval *restrict timeout);
void FD_CLR(int fd, fd_set *fdset);
int FD_ISSET(int fd, fd_set *fdset);
void FD_SET(int fd, fd_set *fdset);
void FD_ZERO(fd_set *fdset);
๐ฆ DESCRIPTION ๐ฆ
so the pselect() function shall examine the file descriptor sets whose addresses are passed in the readfds, writefds, and errorfds parameters ๐ โ checking whether some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively. it's giving "who's ready, sound off" energy fr fr.
the select() function shall be equivalent to the pselect() function, except as follows (and bestie these three differences MATTER):
- for the
select()function, the timeout period is given in seconds and microseconds in an argument of typestruct timeval, whereas for thepselect()function the timeout period is given in seconds and nanoseconds in an argument of typestruct timespec. micro vs nano, ok, glow up. - the
select()function has nosigmaskargument; it shall behave aspselect()does whensigmaskis a null pointer. - upon successful completion, the
select()function may modify the object pointed to by thetimeoutargument. ๐ญ (yes, it can just eat your timeout value, iconic behavior honestly.)
the pselect() and select() functions shall support regular files, terminal and pseudo-terminal devices, STREAMS-based files, FIFOs, pipes, and sockets. the behavior of pselect() and select() on file descriptors that refer to other types of file is unspecified โ so like, do not come crying to me if that goes sideways. ๐
the nfds argument specifies the range of descriptors to be tested. the first nfds descriptors shall be checked in each set; that is, the descriptors from zero through nfds-1 in the descriptor sets shall be examined. ๐ข
if the readfds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to read, and on output indicates which file descriptors are ready to read. ๐ซถ
if the writefds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to write, and on output indicates which file descriptors are ready to write. โ๏ธ
if the errorfds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for error conditions pending, and on output indicates which file descriptors have error conditions pending. ๐จ
upon successful completion, the pselect() or select() function shall modify the objects pointed to by the readfds, writefds, and errorfds arguments to indicate which file descriptors are ready for reading, ready for writing, or have an error condition pending, respectively, and shall return the total number of ready descriptors in all the output sets. for each file descriptor less than nfds, the corresponding bit shall be set upon successful completion if it was set on input and the associated condition is true for that file descriptor. ๐ฏ
if none of the selected descriptors are ready for the requested operation, the pselect() or select() function shall block until at least one of the requested operations becomes ready, until the timeout occurs, or until interrupted by a signal. the timeout parameter controls how long the pselect() or select() function shall take before timing out. if the timeout parameter is not a null pointer, it specifies a maximum interval to wait for the selection to complete. if the specified time interval expires without any requested operation becoming ready, the function shall return. if the timeout parameter is a null pointer, then the call to pselect() or select() shall block indefinitely until at least one descriptor meets the specified criteria. to effect a poll, the timeout parameter should not be a null pointer, and should point to a zero-valued timespec structure. ๐
the use of a timeout does not affect any pending timers set up by alarm() or setitimer(). no cap.
implementations may place limitations on the maximum timeout interval supported. all implementations shall support a maximum timeout interval of at least 31 days. if the timeout argument specifies a timeout interval greater than the implementation-defined maximum value, the maximum value shall be used as the actual timeout value. implementations may also place limitations on the granularity of timeout intervals. if the requested timeout interval requires a finer granularity than the implementation supports, the actual timeout interval shall be rounded up to the next supported value. โฐ
if sigmask is not a null pointer, then the pselect() function shall replace the signal mask of the caller by the set of signals pointed to by sigmask before examining the descriptors, and shall restore the signal mask of the calling thread before returning. the way that it does the swap and swap-back is honestly so clean. ๐ง
a descriptor shall be considered ready for reading when a call to an input function with O_NONBLOCK clear would not block, whether or not the function would transfer data successfully. (the function might return data, an end-of-file indication, or an error other than one indicating that it is blocked, and in each of these cases the descriptor shall be considered ready for reading.) ๐
a descriptor shall be considered ready for writing when a call to an output function with O_NONBLOCK clear would not block, whether or not the function would transfer data successfully. ๐
if a socket has a pending error, it shall be considered to have an exceptional condition pending. otherwise, what constitutes an exceptional condition is file type-specific. for a file descriptor for use with a socket, it is protocol-specific except as noted below. for other file types it is implementation-defined. if the operation is meaningless for a particular file type, pselect() or select() shall indicate that the descriptor is ready for read or write operations, and shall indicate that the descriptor has no exceptional condition pending. ๐
if a descriptor refers to a socket, the implied input function is the recvmsg() function with parameters requesting normal and ancillary data, such that the presence of either type shall cause the socket to be marked as readable. the presence of out-of-band data shall be checked if the socket option SO_OOBINLINE has been enabled, as out-of-band data is enqueued with normal data. if the socket is currently listening, then it shall be marked as readable if an incoming connection request has been received, and a call to the accept() function shall complete without blocking. ๐
if a descriptor refers to a socket, the implied output function is the sendmsg() function supplying an amount of normal data equal to the current value of the SO_SNDLOWAT option for the socket. if a non-blocking call to the connect() function has been made for a socket, and the connection attempt has either succeeded or failed leaving a pending error, the socket shall be marked as writable. ๐
a socket shall be considered to have an exceptional condition pending if a receive operation with O_NONBLOCK clear for the open file description and with the MSG_OOB flag set would return out-of-band data without blocking. (it is protocol-specific whether the MSG_OOB flag would be used to read out-of-band data.) a socket shall also be considered to have an exceptional condition pending if an out-of-band data mark is present in the receive queue. other circumstances under which a socket may be considered to have an exceptional condition pending are protocol-specific and implementation-defined. ๐ฌ
if the readfds, writefds, and errorfds arguments are all null pointers and the timeout argument is not a null pointer, the pselect() or select() function shall block for the time specified, or until interrupted by a signal. if the readfds, writefds, and errorfds arguments are all null pointers and the timeout argument is a null pointer, the pselect() or select() function shall block until interrupted by a signal. basically a fancy nap either way. ๐ด
file descriptors associated with regular files shall always select true for ready to read, ready to write, and error conditions. every time. periodt.
on failure, the objects pointed to by the readfds, writefds, and errorfds arguments shall not be modified. if the timeout interval expires without the specified condition being true for any of the specified file descriptors, the objects pointed to by the readfds, writefds, and errorfds arguments shall have all bits set to 0. ๐งผ
file descriptor masks of type fd_set can be initialized and tested with FD_CLR(), FD_ISSET(), FD_SET(), and FD_ZERO(). it is unspecified whether each of these is a macro or a function. if a macro definition is suppressed in order to access an actual function, or a program defines an external identifier with any of these names, the behavior is undefined. โ ๏ธ
FD_CLR(fd, fdsetp)shall remove the file descriptorfdfrom the set pointed to byfdsetp. iffdis not a member of this set, there shall be no effect on the set, nor will an error be returned. no drama, no error. ๐งนFD_ISSET(fd, fdsetp)shall evaluate to non-zero if the file descriptorfdis a member of the set pointed to byfdsetp, and shall evaluate to zero otherwise. ๐FD_SET(fd, fdsetp)shall add the file descriptorfdto the set pointed to byfdsetp. if the file descriptorfdis already in this set, there shall be no effect on the set, nor will an error be returned. โFD_ZERO(fdsetp)shall initialize the descriptor set pointed to byfdsetpto the null set. no error is returned if the set is not empty at the timeFD_ZERO()is invoked. ๐ซง
bestie. listen. the behavior of these macros is undefined if thefdargument is less than 0 or greater than or equal toFD_SETSIZE, or iffdis not a valid file descriptor, or if any of the arguments are expressions with side-effects. like, undefined undefined. do not do this. ๐
if a thread gets canceled during a pselect() call, the signal mask in effect when executing the registered cleanup functions is either the original signal mask or the signal mask installed as part of the pselect() call. (yep, either one, unspecified, that's just how it is. ๐
)
๐ RETURN VALUE ๐
Okay bestie, here's the tea on what you get back. When everything goes off without a hitch, pselect() and select() hand you the total number of bits set in the bit masks. That's your lil count of ready things. ๐
But if it doesn't go well? You get -1, and errno gets set to tell you what went wrong. Rude, but informative.
Now the lil helper functions:
FD_CLR(),FD_SET(), andFD_ZERO()โ they do not return a value. They're just out here doing the work, no receipts. ๐FD_ISSET()โ returns a non-zero value if the bit for the file descriptorfdis set in the file descriptor set pointed to byfdset, and0otherwise. Basically a vibe check for your fd. โจ
๐ญ ERRORS ๐ญ
Under the following conditions, pselect() and select() shall fail and set errno to:
EBADFโ One or more of the file descriptor sets specified a file descriptor that is not a valid open file descriptor. Bestie that fd does not exist, pls stop. ๐EINTRโ The function was interrupted while blocked waiting for any of the selected descriptors to become ready and before the timeout interval expired. So something poked you mid-nap. ๐ด
IfSA_RESTARThas been set for the interrupting signal, it is implementation-defined whether the function restarts or returns with[EINTR]. So like... it depends on your implementation, no cap.
EINVALโ An invalid timeout interval was specified. That timeout is giving nonsense. ๐ซEINVALโ Thenfdsargument is less than0or greater thanFD_SETSIZE. Out of range, bestie.EINVALโ One of the specified file descriptors refers to aSTREAMor multiplexer that is linked (directly or indirectly) downstream from a multiplexer. Yeah idk either but it's a whole thing.
The following sections are informative.
๐ธ EXAMPLES ๐ธ
None. (I know, I know. Nothing to show off here. ๐ซ )
๐ APPLICATION USAGE ๐
None. (Also nothing! Two for two on the empty sections, iconic behavior honestly. ๐)
๐ง RATIONALE ๐ง
So back in the day โ earlier versions of the Single UNIX Specification โ the select() function was defined in the <sys/time.h> header. That's changed now, it lives in <sys/select.h>. Here's the rationale, bestie, because you deserve to know the lore:
The introduction of the pselect() function included the <sys/select.h> header, and the <sys/select.h> header defines all the related definitions for the pselect() and select() functions. And the backwards-compatibility to existing XSI implementations? Handled by allowing <sys/time.h> to include <sys/select.h>. Everyone wins. ๐
Now, code which wants to avoid the ambiguity of the signal mask for thread cancellation handlers can install an additional cancellation handler which resets the signal mask to the expected value. Like this:
void cleanup(void *arg)
{
sigset_t *ss = (sigset_t *) arg;
pthread_sigmask(SIG_SETMASK, ss, NULL);
}
int call_pselect(int nfds, fd_set *readfds, fd_set *writefds,
fd_set errorfds, const struct timespec *timeout,
const sigset_t *sigmask)
{
sigset_t oldmask;
int result;
pthread_sigmask(SIG_SETMASK, NULL, &oldmask);
pthread_cleanup_push(cleanup, &oldmask);
result = pselect(nfds, readfds, writefds, errorfds, timeout, sigmask);
pthread_cleanup_pop(0);
return result;
}
๐ฎ FUTURE DIRECTIONS ๐ฎ
None. (The future is... empty. Manifesting something better for next edition tho. โจ)
๐ SEE ALSO ๐
Go read these, bestie, they're all in the group chat:
accept(), alarm(), connect(), fcntl(), getitimer(), poll(), read(), recvmsg(), sendmsg(), write()
And from the Base Definitions volume of POSIX.1โ2017: <sys_select.h>, <sys_time.h>
๐ COPYRIGHT ๐
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
And that's a wrap, bestie!! ๐ซถ This whole thing came to you courtesy of IEEE/The Open Group, dated 2017, for the PSELECT(3P) page. Slay, read responsibly, and may all your file descriptors be ready. ๐โจ๐ฆ
๐ฆ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 ยท custom
- alpine 3.23: man-pages-posix 2017a-r0 ยท custom
- alpine 3.24: man-pages-posix 2017a-r0 ยท custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 ยท SUSE-IEEE
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms