syscalls

epoll_wait() — Linux system interface

Retrieve available events from an epoll instance into a caller-owned array. maxevents must be positive and no larger than the array. Timeout is milliseconds, zero is immediate and -1 waits indefinitely. Default level-triggered readiness can repeat until the condition clears. Edge-triggered designs need nonblocking I/O and draining to EAGAIN; EPOLLONESHOT requires rearming. Handle EINTR and inspect error/hangup bits; readiness does not guarantee a subsequent operation succeeds. Compile the complete C example in a fresh private working directory using gcc -std=c17 -Wall -Wextra -Wpedantic -Werror example.c -o example; run ./example. Linux-specific interface or flags; tested on Linux/glibc only. These are libc-facing signatures, not raw architecture-specific syscall ABIs.

Formal Syntax & Structure

linux syntaxlinux
#include <sys/epoll.h>int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);

Parameters & Attributes

NameTypeDescription
epfdintEpoll instance descriptor.
eventsstruct epoll_event *Writable output array.
maxeventsintPositive number of available slots.
timeoutintTimeout in milliseconds.

Return Value

int

Number of events copied, or 0 on timeout. On failure, returns -1 and sets errno.

Working Example

Example Snippetlinux
#define _GNU_SOURCE#define _POSIX_C_SOURCE 200809L#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/epoll.h>#include <fcntl.h>
/* A failed check ends this short-lived demonstration. */#define CHECK(x) do { if (!(x)) { \    fprintf(stderr, "check failed at line %d: %s\n", __LINE__, #x); \    return EXIT_FAILURE; \} } while (0)
int main(void){    int f[2]; char b;    CHECK(pipe2(f, O_NONBLOCK | O_CLOEXEC) == 0);    int e = epoll_create1(EPOLL_CLOEXEC); CHECK(e >= 0);    struct epoll_event ev = { .events=EPOLLIN, .data.fd=f[0] }, out;    CHECK(epoll_ctl(e, EPOLL_CTL_ADD, f[0], &ev) == 0);    CHECK(write(f[1], "r", 1) == 1);    for (int i=0; i<2; ++i) {        CHECK(epoll_wait(e, &out, 1, 1000) == 1);        CHECK(out.data.fd == f[0] && (out.events & EPOLLIN));    }    CHECK(read(f[0], &b, 1) == 1 && b == 'r');    CHECK(read(f[0], &b, 1) == -1 && errno == EAGAIN);    CHECK(epoll_wait(e, &out, 1, 0) == 0);    CHECK(close(f[0]) == 0 && close(f[1]) == 0 && close(e) == 0);    puts("readiness repeated; drained; timeout");    return EXIT_SUCCESS;}
Linux Reference