1POLL(3P) POSIX Programmer's Manual POLL(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 poll - input/output multiplexing
13
15 #include <poll.h>
16
17 int poll(struct pollfd fds[], nfds_t nfds, int timeout);
18
19
21 The poll() function provides applications with a mechanism for multi‐
22 plexing input/output over a set of file descriptors. For each member
23 of the array pointed to by fds, poll() shall examine the given file
24 descriptor for the event(s) specified in events. The number of pollfd
25 structures in the fds array is specified by nfds. The poll() function
26 shall identify those file descriptors on which an application can read
27 or write data, or on which certain events have occurred.
28
29 The fds argument specifies the file descriptors to be examined and the
30 events of interest for each file descriptor. It is a pointer to an
31 array with one member for each open file descriptor of interest. The
32 array's members are pollfd structures within which fd specifies an open
33 file descriptor and events and revents are bitmasks constructed by
34 OR'ing a combination of the following event flags:
35
36 POLLIN Data other than high-priority data may be read without blocking.
37
38 For STREAMS, this flag is set in revents even if the message is of zero
39 length. This flag shall be equivalent to POLLRDNORM | POLLRDBAND.
40
41 POLLRDNORM
42 Normal data may be read without blocking.
43
44 For STREAMS, data on priority band 0 may be read without blocking.
45 This flag is set in revents even if the message is of zero length.
46
47 POLLRDBAND
48 Priority data may be read without blocking.
49
50 For STREAMS, data on priority bands greater than 0 may be read without
51 blocking. This flag is set in revents even if the message is of zero
52 length.
53
54 POLLPRI
55 High-priority data may be read without blocking.
56
57 For STREAMS, this flag is set in revents even if the message is of zero
58 length.
59
60 POLLOUT
61 Normal data may be written without blocking.
62
63 For STREAMS, data on priority band 0 may be written without blocking.
64
65 POLLWRNORM
66 Equivalent to POLLOUT.
67
68 POLLWRBAND
69 Priority data may be written.
70
71 For STREAMS, data on priority bands greater than 0 may be written with‐
72 out blocking. If any priority band has been written to on this STREAM,
73 this event only examines bands that have been written to at least once.
74
75 POLLERR
76 An error has occurred on the device or stream. This flag is only
77 valid in the revents bitmask; it shall be ignored in the events
78 member.
79
80 POLLHUP
81 The device has been disconnected. This event and POLLOUT are
82 mutually-exclusive; a stream can never be writable if a hangup
83 has occurred. However, this event and POLLIN, POLLRDNORM, POLL‐
84 RDBAND, or POLLPRI are not mutually-exclusive. This flag is only
85 valid in the revents bitmask; it shall be ignored in the events
86 member.
87
88 POLLNVAL
89 The specified fd value is invalid. This flag is only valid in
90 the revents member; it shall ignored in the events member.
91
92
93 The significance and semantics of normal, priority, and high-priority
94 data are file and device-specific.
95
96 If the value of fd is less than 0, events shall be ignored, and revents
97 shall be set to 0 in that entry on return from poll().
98
99 In each pollfd structure, poll() shall clear the revents member, except
100 that where the application requested a report on a condition by setting
101 one of the bits of events listed above, poll() shall set the corre‐
102 sponding bit in revents if the requested condition is true. In addi‐
103 tion, poll() shall set the POLLHUP, POLLERR, and POLLNVAL flag in
104 revents if the condition is true, even if the application did not set
105 the corresponding bit in events.
106
107 If none of the defined events have occurred on any selected file
108 descriptor, poll() shall wait at least timeout milliseconds for an
109 event to occur on any of the selected file descriptors. If the value
110 of timeout is 0, poll() shall return immediately. If the value of time‐
111 out is -1, poll() shall block until a requested event occurs or until
112 the call is interrupted.
113
114 Implementations may place limitations on the granularity of timeout
115 intervals. If the requested timeout interval requires a finer granular‐
116 ity than the implementation supports, the actual timeout interval shall
117 be rounded up to the next supported value.
118
119 The poll() function shall not be affected by the O_NONBLOCK flag.
120
121 The poll() function shall support regular files, terminal and pseudo-
122 terminal devices, FIFOs, pipes, sockets and STREAMS-based files. The
123 behavior of poll() on elements of fds that refer to other types of file
124 is unspecified.
125
126 Regular files shall always poll TRUE for reading and writing.
127
128 A file descriptor for a socket that is listening for connections shall
129 indicate that it is ready for reading, once connections are available.
130 A file descriptor for a socket that is connecting asynchronously shall
131 indicate that it is ready for writing, once a connection has been
132 established.
133
135 Upon successful completion, poll() shall return a non-negative value. A
136 positive value indicates the total number of file descriptors that have
137 been selected (that is, file descriptors for which the revents member
138 is non-zero). A value of 0 indicates that the call timed out and no
139 file descriptors have been selected. Upon failure, poll() shall return
140 -1 and set errno to indicate the error.
141
143 The poll() function shall fail if:
144
145 EAGAIN The allocation of internal data structures failed but a subse‐
146 quent request may succeed.
147
148 EINTR A signal was caught during poll().
149
150 EINVAL The nfds argument is greater than {OPEN_MAX}, or one of the fd
151 members refers to a STREAM or multiplexer that is linked
152 (directly or indirectly) downstream from a multiplexer.
153
154
155 The following sections are informative.
156
158 Checking for Events on a Stream
159 The following example opens a pair of STREAMS devices and then waits
160 for either one to become writable. This example proceeds as follows:
161
162 1. Sets the timeout parameter to 500 milliseconds.
163
164 2. Opens the STREAMS devices /dev/dev0 and /dev/dev1, and then polls
165 them, specifying POLLOUT and POLLWRBAND as the events of interest.
166
167 The STREAMS device names /dev/dev0 and /dev/dev1 are only examples of
168 how STREAMS devices can be named; STREAMS naming conventions may vary
169 among systems conforming to the IEEE Std 1003.1-2001.
170
171 3. Uses the ret variable to determine whether an event has occurred on
172 either of the two STREAMS. The poll() function is given 500 mil‐
173 liseconds to wait for an event to occur (if it has not occurred
174 prior to the poll() call).
175
176 4. Checks the returned value of ret. If a positive value is returned,
177 one of the following can be done:
178
179 a. Priority data can be written to the open STREAM on priority
180 bands greater than 0, because the POLLWRBAND event occurred on
181 the open STREAM ( fds[0] or fds[1]).
182
183 b. Data can be written to the open STREAM on priority-band 0,
184 because the POLLOUT event occurred on the open STREAM ( fds[0]
185 or fds[1]).
186
187 5. If the returned value is not a positive value, permission to write
188 data to the open STREAM (on any priority band) is denied.
189
190 6. If the POLLHUP event occurs on the open STREAM ( fds[0] or fds[1]),
191 the device on the open STREAM has disconnected.
192
193 #include <stropts.h>
194 #include <poll.h>
195 ...
196 struct pollfd fds[2];
197 int timeout_msecs = 500;
198 int ret;
199 int i;
200
201
202 /* Open STREAMS device. */
203 fds[0].fd = open("/dev/dev0", ...);
204 fds[1].fd = open("/dev/dev1", ...);
205 fds[0].events = POLLOUT | POLLWRBAND;
206 fds[1].events = POLLOUT | POLLWRBAND;
207
208
209 ret = poll(fds, 2, timeout_msecs);
210
211
212 if (ret > 0) {
213 /* An event on one of the fds has occurred. */
214 for (i=0; i<2; i++) {
215 if (fds[i].revents & POLLWRBAND) {
216 /* Priority data may be written on device number i. */
217 ...
218 }
219 if (fds[i].revents & POLLOUT) {
220 /* Data may be written on device number i. */
221 ...
222 }
223 if (fds[i].revents & POLLHUP) {
224 /* A hangup has occurred on device number i. */
225 ...
226 }
227 }
228 }
229
231 None.
232
234 None.
235
237 None.
238
240 STREAMS, getmsg(), putmsg(), read(), select(), write(), the Base Defi‐
241 nitions volume of IEEE Std 1003.1-2001, <poll.h>, <stropts.h>
242
244 Portions of this text are reprinted and reproduced in electronic form
245 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
246 -- Portable Operating System Interface (POSIX), The Open Group Base
247 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
248 Electrical and Electronics Engineers, Inc and The Open Group. In the
249 event of any discrepancy between this version and the original IEEE and
250 The Open Group Standard, the original IEEE and The Open Group Standard
251 is the referee document. The original Standard can be obtained online
252 at http://www.opengroup.org/unix/online.html .
253
254
255
256IEEE/The Open Group 2003 POLL(3P)