MirBSD manpage: poll(2)

POLL(2)                    BSD Programmer's Manual                     POLL(2)


     poll - synchronous I/O multiplexing


     #include <poll.h>

     poll(struct pollfd *fds, nfds_t nfds, int timeout);


     poll() provides a mechanism for multiplexing I/O across a set of file
     descriptors. It is similar in function to select(2). Unlike select(2),
     however, it is possible to only pass in data corresponding to the file
     descriptors for which events are wanted. This makes poll() more efficient
     than select(2) in most cases.

     The arguments are as follows:

     fds      Points to an array of pollfd structures, which are defined as:

                    struct pollfd {
                            int fd;
                            short events;
                            short revents;

              The fd member is an open file descriptor. If fd is -1, the
              pollfd structure is considered unused, and revents will be

              The events and revents members are bitmasks of conditions to
              monitor and conditions found, respectively.

     nfds     An unsigned integer specifying the number of pollfd structures
              in the array.

     timeout  Maximum interval to wait for the poll to complete, in mil-
              liseconds. If this value is 0, poll() will return immediately.
              If this value is INFTIM (-1), poll() will block indefinitely un-
              til a condition is found.

     The calling process sets the events bitmask and poll() sets the revents
     bitmask. Each call to poll() resets the revents bitmask for accuracy. The
     condition flags in the bitmasks are defined as:

     POLLIN      Data other than high-priority data may be read without block-

     POLLRDNORM  Normal data may be read without blocking.

     POLLRDBAND  Priority data may be read without blocking.

     POLLNORM    Same as POLLRDNORM. This flag is provided for source code
                 compatibility with older programs and should not be used in
                 new code.

     POLLPRI     High-priority data may be read without blocking.

     POLLOUT     Normal data may be written without blocking.


     POLLWRBAND  Priority data may be written.

     POLLERR     An error has occurred on the device or socket. This flag is
                 only valid in the revents bitmask; it is ignored in the
                 events member.

     POLLHUP     The device or socket has been disconnected. This event and
                 POLLOUT are mutually-exclusive; a descriptor can never be
                 writable if a hangup has occurred. However, this event and
                 POLLIN, POLLRDNORM, POLLRDBAND, or POLLPRI are not mutually-
                 exclusive. This flag is only valid in the revents bitmask; it
                 is ignored in the events member.

     POLLNVAL    The corresponding file descriptor is invalid. This flag is
                 only valid in the revents bitmask; it is ignored in the
                 events member.

     The significance and semantics of normal, priority, and high-priority
     data are device-specific.

     In addition to I/O multiplexing, poll() can be used to generate simple
     timeouts. This functionality may be achieved by passing a null pointer
     for fds.


     Upon error, poll() returns -1 and sets the global variable errno to indi-
     cate the error. If the timeout interval was reached before any events oc-
     curred, poll() returns 0. Otherwise, poll() returns the number of file
     descriptors for which revents is non-zero.


     The following example implements a read from the standard input that
     times out after 60 seconds:

           #include <err.h>
           #include <poll.h>
           #include <stdio.h>
           #include <unistd.h>

           struct pollfd pfd[1];
           char buf[BUFSIZ];
           int nfds;

           pfd[0].fd = STDIN_FILENO;
           pfd[0].events = POLLIN;
           nfds = poll(pfd, 1, 60 * 1000);
           if (nfds == -1 || (pfd[0].revents & (POLLERR|POLLHUP|POLLNVAL)))
                   errx(1, "poll error");
           if (nfds == 0)
                   errx(1, "time out");
           if (read(STDIN_FILENO, buf, sizeof(buf)) == -1)
                   errx(1, "read");


     poll() will fail if:

     [EFAULT]   fds points outside the process's allocated address space.

     [EINTR]    poll() caught a signal during the polling process.

     [EINVAL]   nfds was greater than the number of available file descrip-

     [EINVAL]   The timeout passed to poll() was too large.


     getrlimit(2), read(2), select(2), write(2)


     A poll() system call appeared in AT&T System V.3 UNIX.


     The POLLERR and POLLWRBAND flags are accepted but ignored by the kernel.

     Because OpenBSD does not implement STREAMS, there is no distinction
     between some of the fields in the events and revents bitmasks. As a
     result, the POLLIN, POLLNORM, and POLLRDNORM flags are equivalent.

     Internally to the kernel, poll() works poorly if multiple processes wait
     on the same file descriptor.

MirBSD #10-current            December 13, 1994                              2

Generated on 2021-12-07 11:07:08 by $MirOS: src/scripts/roff2htm,v 1.103 2021/01/23 20:24:35 tg Exp $ — This product includes material provided by mirabilos.

These manual pages and other documentation are copyrighted by their respective writers; their sources are available at the project’s CVSweb, AnonCVS and other mirrors. The rest is Copyright © 2002–2021 MirBSD.

This manual page’s HTML representation is supposed to be valid XHTML/1.1; if not, please send a bug report — diffs preferred.