epoll_wait
函数深度解析
epoll_wait
是 Linux epoll I/O 多路复用机制的核心函数,用于等待文件描述符上的 I/O 事件。相比传统的 select()
和 poll()
,它在高并发场景下效率更高。
函数原型
#include <sys/epoll.h>
int epoll_wait(int epfd,
struct epoll_event *events,
int maxevents,
int timeout);
参数详解
-
epfd
- epoll 实例的文件描述符(由
epoll_create1()
创建) - 类型:
int
- 必需参数
- epoll 实例的文件描述符(由
-
events
- 指向
epoll_event
结构数组的指针 - 用于接收就绪事件
- 类型:
struct epoll_event*
- 必需参数
- 指向
-
maxevents
- 指定最多返回的事件数量
- 必须大于 0 且小于等于
events
数组大小 - 类型:
int
- 必需参数
-
timeout
- 等待超时时间(毫秒):
- -1:永久阻塞,直到事件发生
- 0:立即返回,即使没有事件
- >0:等待指定毫秒数
- 类型:
int
- 必需参数
- 等待超时时间(毫秒):
返回值
- 成功:返回就绪的文件描述符数量(0 表示超时)
- 失败:返回 -1,并设置
errno
完整使用案例:高性能 TCP 服务器
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define MAX_EVENTS 64
#define PORT 8080
#define BUFFER_SIZE 1024
// 设置文件描述符为非阻塞模式
void set_nonblocking(int fd) {
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
int main() {
// 创建服务器套接字
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
// 设置 SO_REUSEADDR 避免端口占用
int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
// 绑定地址
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr.s_addr = INADDR_ANY
};
if (bind(server_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("bind failed");
close(server_fd);
exit(EXIT_FAILURE);
}
// 开始监听
if (listen(server_fd, SOMAXCONN) < 0) {
perror("listen failed");
close(server_fd);
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", PORT);
// 创建 epoll 实例
int epoll_fd = epoll_create1(0);
if (epoll_fd < 0) {
perror("epoll_create1 failed");
close(server_fd);
exit(EXIT_FAILURE);
}
// 添加服务器套接字到 epoll
struct epoll_event ev;
ev.events = EPOLLIN; // 监听可读事件(新连接)
ev.data.fd = server_fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server_fd, &ev) < 0) {
perror("epoll_ctl: server_fd");
close(server_fd);
close(epoll_fd);
exit(EXIT_FAILURE);
}
// 事件存储数组
struct epoll_event events[MAX_EVENTS];
// 主事件循环
while (1) {
// 等待事件发生(无限期阻塞)
int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
if (nfds == -1) {
// 处理信号中断
if (errno == EINTR) continue;
perror("epoll_wait failed");
break;
}
// 处理所有就绪事件
for (int i = 0; i < nfds; i++) {
// 1. 处理新连接
if (events[i].data.fd == server_fd) {
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
// 接受新连接
int client_fd = accept(server_fd,
(struct sockaddr*)&client_addr,
&len);
if (client_fd < 0) {
perror("accept failed");
continue;
}
// 设置为非阻塞模式
set_nonblocking(client_fd);
// 添加客户端到 epoll
ev.events = EPOLLIN | EPOLLET; // 边缘触发模式
ev.data.fd = client_fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev) < 0) {
perror("epoll_ctl: client_fd");
close(client_fd);
} else {
printf("New connection: fd=%d\n", client_fd);
}
}
// 2. 处理客户端数据
else {
int client_fd = events[i].data.fd;
// 检查连接关闭或错误
if (events[i].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) {
printf("Connection closed: fd=%d\n", client_fd);
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client_fd, NULL);
close(client_fd);
continue;
}
// 处理可读事件(边缘触发模式)
if (events[i].events & EPOLLIN) {
char buffer[BUFFER_SIZE];
ssize_t bytes_read;
// ET 模式必须循环读取直到 EAGAIN
while (1) {
bytes_read = read(client_fd, buffer, sizeof(buffer));
if (bytes_read > 0) {
// 简单回显处理
write(client_fd, buffer, bytes_read);
}
else if (bytes_read == 0) {
// 对端关闭连接
printf("Client closed connection: fd=%d\n", client_fd);
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client_fd, NULL);
close(client_fd);
break;
}
else {
// 处理错误或非阻塞返回
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// 数据已读完,等待下次事件
break;
}
// 真实错误
perror("read error");
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client_fd, NULL);
close(client_fd);
break;
}
}
}
}
}
}
// 清理资源
close(server_fd);
close(epoll_fd);
return 0;
}
关键实现细节
1. 事件循环结构
while (running) {
int nfds = epoll_wait(epfd, events, MAX_EVENTS, timeout);
for (int i = 0; i < nfds; i++) {
if (events[i].data.fd == server_fd) {
// 处理新连接
} else {
// 处理客户端事件
}
}
}
2. 边缘触发模式处理
// ET 模式必须循环读取直到 EAGAIN
while (1) {
bytes_read = read(fd, buf, sizeof(buf));
if (bytes_read > 0) {
// 处理数据
}
else if (bytes_read == 0) {
// 关闭连接
}
else if (errno == EAGAIN) {
break; // 数据已读完
}
else {
// 处理真实错误
}
}
3. 连接管理
// 添加新连接
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = client_fd;
epoll_ctl(epfd, EPOLL_CTL_ADD, client_fd, &ev);
// 移除关闭的连接
epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
close(fd);
高级使用技巧
1. 超时处理
int timeout_ms = 1000; // 1秒超时
int nfds = epoll_wait(epfd, events, maxevents, timeout_ms);
if (nfds == 0) {
// 执行定时任务(心跳检测、超时清理等)
handle_timeout_tasks();
}
2. 自定义数据结构
struct client_context {
int fd;
struct sockaddr_in addr;
time_t last_active;
};
// 添加连接时
struct client_context *ctx = malloc(sizeof(*ctx));
ctx->fd = client_fd;
ctx->last_active = time(NULL);
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = ctx; // 使用指针存储上下文
epoll_ctl(epfd, EPOLL_CTL_ADD, client_fd, &ev);
// 事件处理时
struct client_context *ctx = events[i].data.ptr;
update_last_active(ctx); // 更新上下文
3. 触发模式混合使用
// 监听套接字使用水平触发(LT)
ev.events = EPOLLIN;
// 数据套接字使用边缘触发(ET)
ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
常见错误及处理
-
忽略 EAGAIN/EWOULDBLOCK
在 ET 模式下必须循环读取直到返回 EAGAIN -
文件描述符泄漏
确保关闭文件描述符前从 epoll 中移除:epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL); close(fd);
-
事件数组溢出
确保maxevents
≤ 实际数组大小 -
未处理信号中断
处理 EINTR 错误:if (nfds == -1 && errno == EINTR) { // 被信号中断,重新等待 continue; }
性能优化建议
-
批量处理事件
利用单次epoll_wait
返回多个事件的特性,减少系统调用次数 -
动态调整事件集
根据负载动态调整监控的文件描述符 -
使用 EPOLLONESHOT
对于需要同步处理的场景:ev.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
- 事件触发后自动禁用监控
- 处理完成后需重新启用
-
避免频繁修改
批量处理 epoll_ctl 操作,减少系统调用
性能对比:在 10K 并发连接测试中,epoll 比 select 快 100 倍以上,且 CPU 使用率低 90%
适用场景
- 高并发网络服务器(Web 服务器、游戏服务器)
- 实时数据处理系统
- 高性能代理/网关
- 需要管理大量 I/O 操作的应用程序
通过合理使用 epoll_wait
和边缘触发模式,可以构建出能够处理数十万并发连接的高性能网络服务,这正是 Nginx、Redis 等高性能服务器的基础。