Medium
accept() at the RLIMIT_NOFILE limit in a level-triggered epoll loop
A server's soft RLIMIT_NOFILE is 1024 and all 1024 descriptors are in use by long-lived client connections. The listening socket is registered with epoll in the default level-triggered mode. A new client now connects and then waits. What does the server process do?
server.cpp
for (;;) {
int n = epoll_wait(ep, events, 64, -1);
for (int i = 0; i < n; ++i) {
if (events[i].data.fd == listen_fd) {
int c = accept4(listen_fd, nullptr, nullptr, SOCK_NONBLOCK);
if (c == -1) {
if (errno == EMFILE) log_ratelimited("out of fds");
continue;
}
add_to_epoll(ep, c);
} else {
handle_client(events[i].data.fd);
}
}
}51 of 53 in Operating Systems