POLL(2)
π POLL β synchronous I/O multiplexing β¨
okay bestie, sit DOWN, because this is the page where your program learns to watch a whole squad of file descriptors at once instead of standing there staring at one like a loser. it's poll, it's in section 2 of the manual, and it is literally the group chat of system calls. π«Ά
π LIBRARY
Standard C Library (libc, -lc) β yeah she lives in libc, obviously. π
β¨ SYNOPSIS β¨
#include <poll.h>
int
poll(struct pollfd fds[], nfds_t nfds, int timeout);
int
ppoll(struct pollfd fds[], nfds_t nfds,
const struct timespec * restrict timeout,
const sigset_t * restrict newsigmask);
π¦ DESCRIPTION π¦
so the poll() system call π looks at a whole set of file descriptors to see if some of them are ready for I/O. like, she's doing a vibe check on your entire roster at once, no cap.
the fds argument is a pointer to an array of pollfd structures, as defined in <poll.h> (shown below, bestie). the nfds argument tells her how big the fds array is.
struct pollfd {
int fd; /* file descriptor */
short events; /* events to look for */
short revents; /* events returned */
};
and the fields of struct pollfd are, one by one:
fdβ the file descriptor you're polling. and here's the tea π: iffdequals-1, thenreventsgets cleared (set to zero) and thatpollfdis not checked at all. she's ghosted, fr fr.eventsβ the events you're polling for. (see below π)reventsβ the events that may actually occur. (see below π)
the event bitmasks in events and revents have these bits, and yes we're listing every single one because nobody gets left out in this house π:
POLLINβ data other than high priority data may be read without blocking. πPOLLRDNORMβ normal data may be read without blocking.POLLRDBANDβ data with a non-zero priority may be read without blocking.POLLPRIβ high priority data may be read without blocking. π¨POLLOUTβ the original page gives this one no description, bestie. she's just standing there. iconic mystery behaviour, iykyk. πPOLLWRNORMβ normal data may be written without blocking. βοΈPOLLWRBANDβ data with a non-zero priority may be written without blocking.POLLERRβ an exceptional condition has occurred on the device or socket. this flag is always checked, even if it's not present in theeventsbitmask. so she shows up uninvited, every time. πPOLLHUPβ the device or socket has been disconnected. this flag is always checked, even if it's not present in theeventsbitmask. and note:POLLHUPandPOLLOUTshould never be present in thereventsbitmask at the same time. not them being mutually exclusive, main character energy. πPOLLNVALβ the file descriptor is not open, or in capability mode the file descriptor has insufficient rights. this flag is always checked, even if it's not present in theeventsbitmask.
okay now the timeout part, this is important so read it twice π₯:
if timeout is neither zero nor INFTIM (-1), it specifies a maximum interval to wait for any file descriptor to become ready, in milliseconds. if timeout is INFTIM (-1), the poll blocks indefinitely β she will wait for you forever, periodt. if timeout is zero, then poll() will return without blocking, she's got places to be. π
πΈ the ppoll() glow-up
the ppoll() system call, unlike poll(), is used to safely wait until either a set of file descriptors becomes ready or until a signal is caught. the fds and nfds arguments are identical to the analogous arguments of poll().
the timeout argument in ppoll() points to a const struct timespec which is defined in <sys/timespec.h> (shown below) rather than the int timeout used by poll(). a null pointer may be passed to indicate that ppoll() should wait indefinitely. and finally, newsigmask specifies a signal mask which is set while waiting for input. when ppoll() returns, the original signal mask is restored. she puts it back exactly how she found it, queen behaviour. π
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* and nanoseconds */
};
π’ RETURN VALUES π’
the poll() system call returns the number of descriptors that are ready for I/O, or -1 if an error occurred. if the time limit expires, poll() returns 0.
and if poll() returns with an error β including one due to an interrupted system call β the fds array will be unmodified. she doesn't leave a mess behind, that's the difference. β¨
π COMPATIBILITY π
this implementation differs from the historical one in that a given file descriptor may not cause poll() to return with an error. in cases where this would have happened in the historical implementation (e.g. trying to poll a revoke(2)ed descriptor), this implementation instead copies the events bitmask to the revents bitmask. attempting to perform I/O on this descriptor will then return an error. this behaviour is believed to be more useful. π«Ά
π ERRORS π
an error return from poll() indicates:
[EFAULT]β thefdsargument points outside the process's allocated address space. she went looking and there was nothing there. π[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.
π SEE ALSO π
accept(2), connect(2), kqueue(2), pselect(2), read(2), recv(2), select(2), send(2), write(2)
go read those too, bestie, they're all in the same friend group. π
β STANDARDS β
the poll() function conforms to IEEE Std 1003.1-2001 (βPOSIX.1β). the ppoll() is not specified by POSIX. she's built different, off-script, no rules. π
π HISTORY π
the poll() function appeared in AT&T System V UNIX. this manual page and the core of the implementation was taken from NetBSD. the ppoll() function first appeared in FreeBSD 11.0.
π BUGS π
the distinction between some of the fields in theeventsandreventsbitmasks is really not useful without STREAMS. the fields are defined for compatibility with existing software. so yeah, some of this is legacy decoration, not that deep, but we keep it anyway because backwards compatibility is a lifestyle. π
and that's the whole page, bestie!! this one came from the Debian crew and it's dated February 27, 2019 β so if you're reading it in your terminal right now, give a lil thank you to the Debian folks who formatted it, mwah ππ go poll something, you got this, i believe in you fr fr β¨π¦πΈ
π¦ 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) 1996 Charles M. Hannum. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms