Lesson 18: Non-blocking I/O and epoll
Blocking I/O is the default — read() waits until data arrives. With O_NONBLOCK, read() returns immediately with EAGAIN if no data is ready. But how do you manage thousands of FDs simultaneously? epoll solves exactly this — an event loop that notifies us only when an FD is ready, in O(1). NVIDIA's Tr
Blocking I/O is standing in a ticket line and waiting. Non-blocking is telling the clerk 'call me when ready'. epoll is a switchboard operator managing thousands of calls, forwarding only the ones waiting for your response.
- non-blocking I/O
- Mode where I/O calls (read, write, accept) do not block. If no data is ready, they return -1 with errno=EAGAIN/EWOULDBLOCK. Enabled with the O_NONBLOCK flag.
- epoll
- Linux interface for monitoring I/O events on a large set of file descriptors. Comprises epoll_create, epoll_ctl, and epoll_wait. Returns only ready FDs — O(1) vs O(n) for select.
- edge-triggered (ET)
- epoll mode where an event is reported only once — when the FD transitions from not-ready to ready. Requires reading until EAGAIN each time. More efficient but more complex.
- level-triggered (LT)
- Default epoll mode — event is reported every time epoll_wait is called while the FD is still ready. Simpler than ET but can cause busy-looping if not handled properly.
- event loop
- A programming pattern where a program waits for events and handles them one by one — an infinite loop calling epoll_wait and firing callbacks. The core of Node.js, Nginx, and Triton.