POLL(3POSIX)
π POLL(3POSIX) β bestie edition π
okay so this page is poll(3POSIX), straight out of the POSIX Programmer's Manual. pull up a chair, grab a boba, we're doing this. π§β¨
πΈ PROLOG πΈ
okay so heads up before we even start: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β like, actually differ β so go consult the corresponding Linux manual page for the real Linux tea. or, plot twist, the interface might not even be implemented on Linux at all. π
β¨ NAME β¨
poll β input/output multiplexing. that's the whole vibe. it's giving "watch a whole bunch of file descriptors at once and tell me which ones are ready." π
π¦ SYNOPSIS π¦
#include <poll.h>
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
π DESCRIPTION π
so the poll() function hands applications a whole mechanism for multiplexing input/output over a set of file descriptors. for each member of the array pointed to by fds, poll() shall examine the given file descriptor for the event(s) specified in events. the number of pollfd structures chilling in the fds array is specified by nfds. and poll() shall identify those file descriptors on which an application can read or write data, or on which certain events have occurred. main character energy fr fr. π
the fds argument specifies the file descriptors to be examined and the events of interest for each file descriptor. it's a pointer to an array with one member for each open file descriptor of interest. the array's members are pollfd structures, within which fd specifies an open file descriptor, and events and revents are bitmasks constructed by OR'ing a combination of the following event flags. no cap:
POLLINβ data other than high-priority data may be read without blocking. for STREAMS, this flag is set inreventseven if the message is of zero length. this flag shall be equivalent toPOLLRDNORM | POLLRDBAND.POLLRDNORMβ normal data may be read without blocking. for STREAMS, data on priority band 0 may be read without blocking. this flag is set inreventseven if the message is of zero length.POLLRDBANDβ priority data may be read without blocking. for STREAMS, data on priority bands greater than 0 may be read without blocking. this flag is set inreventseven if the message is of zero length.POLLPRIβ high-priority data may be read without blocking. for STREAMS, this flag is set inreventseven if the message is of zero length.POLLOUTβ normal data may be written without blocking. for STREAMS, data on priority band 0 may be written without blocking.POLLWRNORMβ equivalent toPOLLOUT. that's it, that's the whole flag.POLLWRBANDβ priority data may be written. for STREAMS, data on priority bands greater than 0 may be written without blocking. if any priority band has been written to on this STREAM, this event only examines bands that have been written to at least once.POLLERRβ an error has occurred on the device or stream. β οΈ this flag is only valid in thereventsbitmask; it shall be ignored in theeventsmember.POLLHUPβ a device has been disconnected, or a pipe or FIFO has been closed by the last process that had it open for writing. once set, the hangup state of a FIFO shall persist until some process opens the FIFO for writing, or until all read-only file descriptors for the FIFO are closed. this event andPOLLOUTare mutually-exclusive β a stream can never be writable if a hangup has occurred. however, this event andPOLLIN,POLLRDNORM,POLLRDBAND, orPOLLPRIare not mutually-exclusive. this flag is only valid in thereventsbitmask; it shall be ignored in theeventsmember.POLLNVALβ the specifiedfdvalue is invalid. this flag is only valid in thereventsmember; it shall be ignored in theeventsmember.
the significance and semantics of normal, priority, and high-priority data are file and device-specific. iykyk. π€·ββοΈ
if the value of fd is less than 0, events shall be ignored, and revents shall be set to 0 in that entry on return from poll().
in each pollfd structure, poll() shall clear the revents member β except that where the application requested a report on a condition by setting one of the bits of events listed above, poll() shall set the corresponding bit in revents if the requested condition is true. plus, poll() shall set the POLLHUP, POLLERR, and POLLNVAL flags in revents if the condition is true, even if the application did not set the corresponding bit in events. sneaky but correct. π«Ά
if none of the defined events have occurred on any selected file descriptor, poll() shall wait at least timeout milliseconds for an event to occur on any of the selected file descriptors. and bestie, listen:
- if the value of
timeoutis 0,poll()shall return immediately. β‘ - if the value of
timeoutis -1,poll()shall block until a requested event occurs or until the call is interrupted. π΄
implementations may 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.
the poll() function shall not be affected by the O_NONBLOCK flag. nope, not today. π
the poll() function shall support regular files, terminal and pseudo-terminal devices, FIFOs, pipes, sockets and STREAMS-based files. the behavior of poll() on elements of fds that refer to other types of file is unspecified. π
regular files shall always poll TRUE for reading and writing. iconic.
a file descriptor for a socket that is listening for connections shall indicate that it is ready for reading, once connections are available. a file descriptor for a socket that is connecting asynchronously shall indicate that it is ready for writing, once a connection has been established. slay. π₯
provided the application does not perform any action that results in unspecified or undefined behavior, the value of the fd and events members of each element of fds shall not be modified by poll().
π RETURN VALUE π
upon successful completion, poll() shall return a non-negative value.
- a positive value indicates the total number of
pollfdstructures that have selected events β that is, those for which thereventsmember is non-zero. - a value of 0 indicates that the call timed out and no file descriptors have been selected.
- upon failure,
poll()shall return -1 and seterrnoto indicate the error. π
π ERRORS π
the poll() function shall fail if:
EAGAINβ the allocation of internal data structures failed but a subsequent request may succeed.EINTRβ a signal was caught duringpoll().EINVALβ thenfdsargument is greater than{OPEN_MAX}, or one of thefdmembers refers to a STREAM or multiplexer that is linked (directly or indirectly) downstream from a multiplexer.
the following sections are informative.
π EXAMPLES π
Checking for Events on a Stream
the following example opens a pair of STREAMS devices and then waits for either one to become writable. it proceeds like this:
- sets the
timeoutparameter to 500 milliseconds. - opens the STREAMS devices
/dev/dev0and/dev/dev1, and then polls them, specifyingPOLLOUTandPOLLWRBANDas the events of interest.
the STREAMS device names /dev/dev0 and /dev/dev1 are only examples of how STREAMS devices can be named; STREAMS naming conventions may vary among systems conforming to the POSIX.1β2008.
- uses the
retvariable to determine whether an event has occurred on either of the two STREAMS. thepoll()function is given 500 milliseconds to wait for an event to occur (if it has not occurred prior to thepoll()call). - checks the returned value of
ret. if a positive value is returned, one of the following can be done:- priority data can be written to the open STREAM on priority bands greater than 0, because the
POLLWRBANDevent occurred on the open STREAM (fds[0]orfds[1]). - data can be written to the open STREAM on priority-band 0, because the
POLLOUTevent occurred on the open STREAM (fds[0]orfds[1]).
- priority data can be written to the open STREAM on priority bands greater than 0, because the
- if the returned value is not a positive value, permission to write data to the open STREAM (on any priority band) is denied.
- if the
POLLHUPevent occurs on the open STREAM (fds[0]orfds[1]), the device on the open STREAM has disconnected. π
#include <stropts.h>
#include <poll.h>
...
struct pollfd fds[2];
int timeout_msecs = 500;
int ret;
int i;
/* Open STREAMS device. */
fds[0].fd = open("/dev/dev0", ...);
fds[1].fd = open("/dev/dev1", ...);
fds[0].events = POLLOUT | POLLWRBAND;
fds[1].events = POLLOUT | POLLWRBAND;
ret = poll(fds, 2, timeout_msecs);
if (ret > 0) {
/* An event on one of the fds has occurred. */
for (i=0; i<2; i++) {
if (fds[i].revents & POLLWRBAND) {
/* Priority data may be written on device number i. */
...
}
if (fds[i].revents & POLLOUT) {
/* Data may be written on device number i. */
...
}
if (fds[i].revents & POLLHUP) {
/* A hangup has occurred on device number i. */
...
}
}
}
π APPLICATION USAGE π
None. literally nothing. move along bestie. πΆββοΈ
π§ RATIONALE π§
the POLLHUP event does not occur for FIFOs just because the FIFO is not open for writing. it only occurs when the FIFO is closed by the last writer, and it persists until some process opens the FIFO for writing or until all read-only file descriptors for the FIFO are closed. important distinction, don't get it twisted. π
β FUTURE DIRECTIONS β
None.
π SEE ALSO π
- Section 2.6, STREAMS
getmsg()pselect()putmsg()read()write()
the Base Definitions volume of POSIX.1β2017: <poll.h>, <stropts.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 the whole page, bestie!! π this absolute banger of a page was brought to you by IEEE / The Open Group, 2017 β yeah, 2017, she's a little vintage but she still eats. now go multiplex some file descriptors, you got this. mwah. πβ¨π¦
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms