1 .\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved
2 .TH "POLL" P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
5 poll \- input/output multiplexing
11 int poll(struct pollfd\fP \fIfds\fP\fB[], nfds_t\fP \fInfds\fP\fB,
12 int\fP \fItimeout\fP\fB); \fP
18 The \fIpoll\fP() function provides applications with a mechanism for
19 multiplexing input/output over a set of file descriptors.
20 For each member of the array pointed to by \fIfds\fP, \fIpoll\fP()
21 shall examine the given file descriptor for the event(s)
22 specified in \fIevents\fP. The number of \fBpollfd\fP structures in
23 the \fIfds\fP array is specified by \fInfds\fP. The
24 \fIpoll\fP() function shall identify those file descriptors on which
25 an application can read or write data, or on which certain
28 The \fIfds\fP argument specifies the file descriptors to be examined
29 and the events of interest for each file descriptor. It is
30 a pointer to an array with one member for each open file descriptor
31 of interest. The array's members are \fBpollfd\fP structures
32 within which \fIfd\fP specifies an open file descriptor and \fIevents\fP
33 and \fIrevents\fP are bitmasks constructed by OR'ing a
34 combination of the following event flags:
37 Data other than high-priority data may be read without blocking.
39 For STREAMS, this flag is set in \fIrevents\fP even if the message
40 is of zero length. This flag shall be equivalent to POLLRDNORM
44 Normal data may be read without blocking.
46 For STREAMS, data on priority band 0 may be read without blocking.
47 This flag is set in \fIrevents\fP even if the message is of
51 Priority data may be read without blocking.
53 For STREAMS, data on priority bands greater than 0 may be read without
54 blocking. This flag is set in \fIrevents\fP even if the
55 message is of zero length.
58 High-priority data may be read without blocking.
60 For STREAMS, this flag is set in \fIrevents\fP even if the message
64 Normal data may be written without blocking.
66 For STREAMS, data on priority band 0 may be written without blocking.
69 Equivalent to POLLOUT.
72 Priority data may be written.
74 For STREAMS, data on priority bands greater than 0 may be written
75 without blocking. If any priority band has been written to on
76 this STREAM, this event only examines bands that have been written
80 An error has occurred on the device or stream. This flag is only valid
81 in the \fIrevents\fP bitmask; it shall be ignored in
82 the \fIevents\fP member.
85 The device has been disconnected. This event and POLLOUT are mutually-exclusive;
86 a stream can never be writable if a hangup has
87 occurred. However, this event and POLLIN, POLLRDNORM, POLLRDBAND,
88 or POLLPRI are not mutually-exclusive. This flag is only valid in
89 the \fIrevents\fP bitmask; it shall be ignored in the \fIevents\fP
93 The specified \fIfd\fP value is invalid. This flag is only valid in
94 the \fIrevents\fP member; it shall ignored in the
98 The significance and semantics of normal, priority, and high-priority
99 data are file and device-specific.
101 If the value of \fIfd\fP is less than 0, \fIevents\fP shall be ignored,
102 and \fIrevents\fP shall be set to 0 in that entry on
103 return from \fIpoll\fP().
105 In each \fBpollfd\fP structure, \fIpoll\fP() shall clear the \fIrevents\fP
106 member, except that where the application
107 requested a report on a condition by setting one of the bits of \fIevents\fP
108 listed above, \fIpoll\fP() shall set the
109 corresponding bit in \fIrevents\fP if the requested condition is true.
110 In addition, \fIpoll\fP() shall set the POLLHUP, POLLERR,
111 and POLLNVAL flag in \fIrevents\fP if the condition is true, even
112 if the application did not set the corresponding bit in
115 If none of the defined events have occurred on any selected file descriptor,
116 \fIpoll\fP() shall wait at least \fItimeout\fP
117 milliseconds for an event to occur on any of the selected file descriptors.
118 If the value of \fItimeout\fP is 0, \fIpoll\fP()
119 shall return immediately. If the value of \fItimeout\fP is -1, \fIpoll\fP()
120 shall block until a requested event occurs or until
121 the call is interrupted.
123 Implementations may place limitations on the granularity of timeout
124 intervals. If the requested timeout interval requires a
125 finer granularity than the implementation supports, the actual timeout
126 interval shall be rounded up to the next supported
129 The \fIpoll\fP() function shall not be affected by the O_NONBLOCK
132 The \fIpoll\fP() function shall support regular files, terminal and
133 pseudo-terminal devices, FIFOs, pipes, sockets and
134 \ STREAMS-based files. The behavior of \fIpoll\fP() on
135 elements of \fIfds\fP that refer to other types of file is unspecified.
137 Regular files shall always poll TRUE for reading and writing.
139 A file descriptor for a socket that is listening for connections shall
140 indicate that it is ready for reading, once connections
141 are available. A file descriptor for a socket that is connecting asynchronously
142 shall indicate that it is ready for writing, once a
143 connection has been established.
146 Upon successful completion, \fIpoll\fP() shall return a non-negative
147 value. A positive value indicates the total number of file
148 descriptors that have been selected (that is, file descriptors for
149 which the \fIrevents\fP member is non-zero). A value of 0
150 indicates that the call timed out and no file descriptors have been
151 selected. Upon failure, \fIpoll\fP() shall return -1 and set
152 \fIerrno\fP to indicate the error.
155 The \fIpoll\fP() function shall fail if:
158 The allocation of internal data structures failed but a subsequent
162 A signal was caught during \fIpoll\fP().
165 The \fInfds\fP argument is greater than {OPEN_MAX}, \ or one of
166 the \fIfd\fP members refers to a STREAM or multiplexer
167 that is linked (directly or indirectly) downstream from a multiplexer.
170 \fIThe following sections are informative.\fP
172 .SS Checking for Events on a Stream
174 The following example opens a pair of STREAMS devices and then waits
175 for either one to become writable. This example proceeds as
178 Sets the \fItimeout\fP parameter to 500 milliseconds.
181 Opens the STREAMS devices \fB/dev/dev0\fP and \fB/dev/dev1\fP, and
182 then polls them, specifying POLLOUT and POLLWRBAND as the
185 The STREAMS device names \fB/dev/dev0\fP and \fB/dev/dev1\fP are only
186 examples of how STREAMS devices can be named; STREAMS
187 naming conventions may vary among systems conforming to the IEEE\ Std\ 1003.1-2001.
190 Uses the \fIret\fP variable to determine whether an event has occurred
191 on either of the two STREAMS. The \fIpoll\fP() function
192 is given 500 milliseconds to wait for an event to occur (if it has
193 not occurred prior to the \fIpoll\fP() call).
196 Checks the returned value of \fIret\fP. If a positive value is returned,
197 one of the following can be done:
200 Priority data can be written to the open STREAM on priority bands
201 greater than 0, because the POLLWRBAND event occurred on the
202 open STREAM ( \fIfds\fP[0] or \fIfds\fP[1]).
205 Data can be written to the open STREAM on priority-band 0, because
206 the POLLOUT event occurred on the open STREAM ( \fIfds\fP[0]
212 If the returned value is not a positive value, permission to write
213 data to the open STREAM (on any priority band) is denied.
216 If the POLLHUP event occurs on the open STREAM ( \fIfds\fP[0] or \fIfds\fP[1]),
217 the device on the open STREAM has
224 \fB#include <stropts.h>
227 struct pollfd fds[2];
228 int timeout_msecs = 500;
233 /* Open STREAMS device. */
234 fds[0].fd = open("/dev/dev0", ...);
235 fds[1].fd = open("/dev/dev1", ...);
236 fds[0].events = POLLOUT | POLLWRBAND;
237 fds[1].events = POLLOUT | POLLWRBAND;
240 ret = poll(fds, 2, timeout_msecs);
244 /* An event on one of the fds has occurred. */
245 for (i=0; i<2; i++) {
246 if (fds[i].revents & POLLWRBAND) {
247 /* Priority data may be written on device number i. */
250 if (fds[i].revents & POLLOUT) {
251 /* Data may be written on device number i. */
254 if (fds[i].revents & POLLHUP) {
255 /* A hangup has occurred on device number i. */
263 .SH APPLICATION USAGE
269 .SH FUTURE DIRECTIONS
274 \fISTREAMS\fP , \fIgetmsg\fP() , \fIputmsg\fP() , \fIread\fP() , \fIselect\fP()
275 , \fIwrite\fP() , the Base Definitions volume of IEEE\ Std\ 1003.1-2001,
276 \fI<poll.h>\fP, \fI<stropts.h>\fP
278 Portions of this text are reprinted and reproduced in electronic form
279 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
280 -- Portable Operating System Interface (POSIX), The Open Group Base
281 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
282 Electrical and Electronics Engineers, Inc and The Open Group. In the
283 event of any discrepancy between this version and the original IEEE and
284 The Open Group Standard, the original IEEE and The Open Group Standard
285 is the referee document. The original Standard can be obtained online at
286 http://www.opengroup.org/unix/online.html .