epoll_ctl()¶
Purpose¶
Adds, modifies, or removes a file descriptor from an epoll interest list.
Header¶
Prototype¶
Parameters¶
epfd: epoll instance fd.op:EPOLL_CTL_ADD,EPOLL_CTL_MOD, orEPOLL_CTL_DEL.fd: target fd to monitor.event: event mask and user data. UseNULLforEPOLL_CTL_DELon Linux.
Return Value¶
- Success: returns
0. - Failure: returns
-1and setserrno.
Minimal Example¶
struct epoll_event ev = {0};
ev.events = EPOLLIN;
ev.data.fd = fd;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
perror("epoll_ctl");
}
Common Pitfalls¶
- Do not forget to initialize
struct epoll_event. - Use
EPOLL_CTL_MODinstead of adding the same fd twice. - Handle
EPOLLERRandEPOLLHUPeven if they are not explicitly requested.