Medium
Edge-triggered epoll with a single read() per event
client_fd is registered with EPOLLET (edge-triggered), as shown below. A burst of 2000 bytes arrives at once on client_fd, but the handler performs a single 512-byte read() and returns to epoll_wait() without looping until read() returns EAGAIN. No further data ever arrives on client_fd afterward. What happens to the remaining ~1488 bytes still sitting in the kernel's socket receive buffer?
server.cpp
#include <sys/epoll.h>
#include <unistd.h>
#include <cstdio>
int event_loop(int epfd) {
epoll_event events[16];
for (;;) {
int n = epoll_wait(epfd, events, 16, -1);
for (int i = 0; i < n; ++i) {
char buf[512];
// Single read, NOT a loop to EAGAIN.
ssize_t r = read(events[i].data.fd, buf, sizeof(buf));
if (r <= 0) continue;
// ... process buf[0..r) ...
}
}
}38 of 53 in Operating Systems