Skip to content
Tutorials Center
My learning

Topics

C Reference ยท POSIX and Systems

C select() and poll() functions

Reference for select() and poll(): wait for several file descriptors at once, use fd_set macros and struct pollfd, set timeouts, and avoid common pitfalls.

A program that reads from several sources (many sockets, a pipe and the keyboard) cannot simply call read on each, because the first call would block while the others have data. select() and poll() solve this: you give the kernel a set of descriptors and a timeout, and the call returns when at least one is ready (or the time is up). You then read only from the ready ones, without blocking.

select is older and universal but limited to descriptors below FD_SETSIZE (usually 1024) and rebuilds its sets on each call. poll takes an array of struct pollfd, has no such limit and separates input from output. For thousands of descriptors Linux offers epoll, BSD and macOS offer kqueue.

C
#include <poll.h>

struct pollfd p = { .fd = fd, .events = POLLIN };
int ready = poll(&p, 1, 1000);        /* wait up to 1000 ms */

Try it

C poll() on two pipes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C select() with a timeout

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
#include <sys/select.h>
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
           struct timeval *timeout);
void FD_ZERO(fd_set *set);
void FD_SET(int fd, fd_set *set);
void FD_CLR(int fd, fd_set *set);
int  FD_ISSET(int fd, fd_set *set);

#include <poll.h>
int poll(struct pollfd fds[], nfds_t nfds, int timeout);

struct pollfd { int fd; short events; short revents; };
Name Description
nfds (select) The highest descriptor number plus one.
readfds, writefds, exceptfds Sets of descriptors to test for readability, writability and exceptional conditions; each may be NULL. They are overwritten with the ready subset.
timeout (select) Maximum wait as struct timeval; NULL waits forever; {0,0} returns at once.
fds, nfds (poll) Array and number of entries. Negative fd entries are ignored.
events Requested conditions: POLLIN, POLLOUT, POLLPRI.
revents Filled by poll; may also contain POLLERR, POLLHUP, POLLNVAL.
timeout (poll) Milliseconds; -1 waits forever, 0 returns at once.

Returns: the number of ready descriptors (select: the total count of bits set), 0 on timeout, -1 on error (EINTR, EBADF, EINVAL).

Usage notes

  • Ready means "will not block", not "has a full message". After POLLIN, read returns at least one byte, or 0 for end of file. A closed peer also makes the descriptor readable, so always handle read() == 0.
  • select modifies its sets. Rebuild them (or copy a master set) before every call. poll keeps events and rewrites only revents.
  • Forgetting + 1 in nfds for select silently ignores the highest descriptor. Using a descriptor >= FD_SETSIZE with the FD_ macros is undefined behavior.
  • On Linux select updates timeout with the time left; other systems do not. Never reuse it without resetting.
  • POLLHUP signals that the other end was closed; POLLNVAL means the descriptor is not open (a bug in your program).
  • Both calls can be interrupted by a signal and fail with EINTR; retry with a recomputed timeout.
  • Cost: each call is linear in the number of descriptors. That is fine for hundreds, poor for tens of thousands (use epoll/kqueue).
  • Combine with non-blocking descriptors (O_NONBLOCK) to avoid blocking on a partial write.
  • pselect and ppoll additionally set a signal mask atomically, which avoids a race between checking a signal flag and sleeping.