epoll API Reference
Core APIs
int epoll_create1(int flags);
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
Common Flags
| Flag |
Usage |
EPOLL_CLOEXEC |
Close epoll fd across exec() |
EPOLLIN |
fd is readable |
EPOLLOUT |
fd is writable |
EPOLLERR |
error condition |
EPOLLHUP |
hang-up condition |
EPOLLET |
edge-triggered mode |
Common Operations
| Operation |
Purpose |
EPOLL_CTL_ADD |
Add fd to epoll set |
EPOLL_CTL_MOD |
Modify monitored events |
EPOLL_CTL_DEL |
Remove fd from epoll set |
Return Value
| API |
Success |
Failure |
epoll_create1() |
epoll fd |
-1, sets errno |
epoll_ctl() |
0 |
-1, sets errno |
epoll_wait() |
number of ready events |
-1, sets errno |
Minimal Example
int epfd = epoll_create1(EPOLL_CLOEXEC);
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = fd;
epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
Common Pitfalls
- Use non-blocking fds in event loops.
- Handle
EINTR from epoll_wait().
- Handle
EPOLLERR and EPOLLHUP.
- Remove or close fds carefully to avoid stale ownership.