主题
epoll_create / epoll_ctl / epoll_wait · 如何使用
可运行 Demo
demo 模仿 lmkd mainloop 的分发模型:创建两个 pipe,用 epoll 同时监听两个 读端(LT 模式),向两个 pipe 各写一条消息后,一次 epoll_wait 返回两个 就绪事件,逐个读取打印;随后把 pipe1 改为 EPOLLET,演示边缘触发与 水平触发的行为差异。完整源码见 demos/epoll_demo.c:
c
/*
* epoll_demo.c —— epoll_create / epoll_ctl / epoll_wait 演示
*
* 场景:模仿 lmkd mainloop() 的事件分发——一个线程(进程)通过 epoll
* 同时监听多个 fd:
* 阶段一(水平触发 LT,默认):创建两个 pipe,父进程向两个 pipe 各写
* 一条消息,一次 epoll_wait 返回两个就绪事件,逐个读取打印。
* 阶段二(边缘触发 ET):把 pipe1 读端改为 EPOLLET,写入一条消息后
* 只读 1 字节——剩余的未读数据不会再次触发 epoll_wait(超时返回 0),
* 直到有新数据写入(状态再次变化)才会再次收到通知。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/epoll.h>
#define MAX_EVENTS 2 /* 对应 lmkd 的 MAX_EPOLL_EVENTS:一次最多取回的就绪事件数 */
static void die(const char *msg) {
perror(msg);
exit(EXIT_FAILURE);
}
/* 从 fd 读数据并打印(LT 模式下可以只读一次,未读完下次还会再通知) */
static void read_and_print(const char *tag, int fd) {
char buf[128];
ssize_t n = read(fd, buf, sizeof(buf) - 1);
if (n < 0)
die("read");
buf[n] = '\0';
printf(" [就绪] %s (fd=%d) 读到 %zd 字节: \"%s\"\n", tag, fd, n, buf);
}
int main(void) {
int pipe1[2], pipe2[2];
int epfd;
struct epoll_event ev, events[MAX_EVENTS];
int i, n;
if (pipe(pipe1) == -1 || pipe(pipe2) == -1)
die("pipe");
/* 1. 创建 epoll 实例(lmkd: epoll_create(MAX_EPOLL_EVENTS)) */
epfd = epoll_create(MAX_EVENTS);
if (epfd == -1)
die("epoll_create");
printf("[1] epoll_create 成功, epoll fd = %d\n", epfd);
/* 2. 把两个 pipe 的读端注册进 epoll,关注 EPOLLIN(可读)事件。
* 通过 data.u32 存一个编号,事件到来时据此区分是哪个 pipe。 */
ev.events = EPOLLIN; /* LT:默认即水平触发,无需额外标志 */
ev.data.u32 = 1;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, pipe1[0], &ev) == -1)
die("epoll_ctl pipe1");
ev.data.u32 = 2;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, pipe2[0], &ev) == -1)
die("epoll_ctl pipe2");
printf("[2] epoll_ctl 注册 pipe1 读端 (fd=%d) 和 pipe2 读端 (fd=%d), 事件=EPOLLIN(LT)\n",
pipe1[0], pipe2[0]);
/* 3. 向两个 pipe 各写一条消息,制造“两个 fd 同时就绪”的场面 */
if (write(pipe1[1], "hello from pipe1", 16) != 16)
die("write pipe1");
if (write(pipe2[1], "hello from pipe2", 16) != 16)
die("write pipe2");
printf("[3] 已向两个 pipe 各写入一条消息\n");
/* 4. epoll_wait 一次返回全部就绪事件,逐个处理(对应 lmkd mainloop 的 for 循环) */
n = epoll_wait(epfd, events, MAX_EVENTS, -1); /* -1 = 无限期阻塞 */
if (n == -1)
die("epoll_wait");
printf("[4] epoll_wait 返回 %d 个就绪事件:\n", n);
for (i = 0; i < n; i++) {
if (events[i].data.u32 == 1)
read_and_print("pipe1", pipe1[0]);
else if (events[i].data.u32 == 2)
read_and_print("pipe2", pipe2[0]);
}
/* ---- 阶段二:边缘触发(ET)演示 ---- */
printf("\n[5] 把 pipe1 读端改为 EPOLLET(边缘触发)\n");
ev.events = EPOLLIN | EPOLLET;
ev.data.u32 = 1;
if (epoll_ctl(epfd, EPOLL_CTL_MOD, pipe1[0], &ev) == -1)
die("epoll_ctl MOD");
if (write(pipe1[1], "ET-demo-message", 15) != 15)
die("write pipe1 ET");
printf("[6] 向 pipe1 写入 15 字节 \"ET-demo-message\"\n");
/* ET:状态变化(空->有数据)触发一次通知 */
n = epoll_wait(epfd, events, MAX_EVENTS, -1);
if (n == -1)
die("epoll_wait ET");
printf("[7] epoll_wait(ET) 返回 %d 个事件,这次只读 1 字节: ", n);
{
char c;
if (read(pipe1[0], &c, 1) != 1)
die("read 1 byte");
printf("读到 '%c',还剩 14 字节未读\n", c);
}
/* 关键区别:剩余数据还在缓冲区里,但 ET 不会再次通知 —— 300ms 超时返回 0 */
n = epoll_wait(epfd, events, MAX_EVENTS, 300);
if (n == -1)
die("epoll_wait ET again");
printf("[8] 缓冲区仍有 14 字节未读,再次 epoll_wait(ET, timeout=300ms) 返回 %d —— 不会重复通知\n", n);
/* 若是 LT,只要缓冲区有数据就会一直通知;演示一下对比 */
ev.events = EPOLLIN; /* 改回 LT */
if (epoll_ctl(epfd, EPOLL_CTL_MOD, pipe1[0], &ev) == -1)
die("epoll_ctl MOD back to LT");
n = epoll_wait(epfd, events, MAX_EVENTS, 300);
if (n == -1)
die("epoll_wait LT again");
printf("[9] 改回 LT 后 epoll_wait 立即返回 %d —— 只要还有数据就会通知\n", n);
read_and_print("pipe1 剩余数据", pipe1[0]);
close(pipe1[0]); close(pipe1[1]);
close(pipe2[0]); close(pipe2[1]);
close(epfd);
return 0;
}LT(水平触发)与 ET(边缘触发)的区别
| LT(默认) | ET(EPOLLET) | |
|---|---|---|
| 通知时机 | 只要 fd 处于就绪状态(缓冲区还有数据),每次 epoll_wait 都会通知 | 只在状态「变化」的瞬间通知一次(如从无到有新数据到达) |
| 没读完的数据 | 下次 epoll_wait 还会再通知,不容易丢事件 | 不会重复通知,必须在本次把数据读到 EAGAIN,否则剩余数据可能永远拿不到 |
| 编程要求 | 简单,阻塞 fd 也能用(lmkd 就是 LT) | 通常必须配合非阻塞 fd + 循环 read/write 直到 EAGAIN |
| 效率 | 数据没处理完会反复唤醒 | 通知次数少,适合高并发;epoll 高效用法一般指 ET |
demo 的阶段二对此有直观演示:ET 模式下写入 15 字节只读 1 字节,剩余 14 字节留在缓冲区,再次 epoll_wait 超时返回 0(不重复通知);改回 LT 后 epoll_wait 立即返回(只要还有数据就通知)。
编译命令与运行输出
c
$ gcc -Wall -O2 -o /tmp/epoll_demo /opt/www.fukaiqiang.com/demos/epoll_demo.c
$ /tmp/epoll_demo
[1] epoll_create 成功, epoll fd = 7
[2] epoll_ctl 注册 pipe1 读端 (fd=3) 和 pipe2 读端 (fd=5), 事件=EPOLLIN(LT)
[3] 已向两个 pipe 各写入一条消息
[4] epoll_wait 返回 2 个就绪事件:
[就绪] pipe1 (fd=3) 读到 16 字节: "hello from pipe1"
[就绪] pipe2 (fd=5) 读到 16 字节: "hello from pipe2"
[5] 把 pipe1 读端改为 EPOLLET(边缘触发)
[6] 向 pipe1 写入 15 字节 "ET-demo-message"
[7] epoll_wait(ET) 返回 1 个事件,这次只读 1 字节: 读到 'E',还剩 14 字节未读
[8] 缓冲区仍有 14 字节未读,再次 epoll_wait(ET, timeout=300ms) 返回 0 —— 不会重复通知
[9] 改回 LT 后 epoll_wait 立即返回 1 —— 只要还有数据就会通知
[就绪] pipe1 剩余数据 (fd=3) 读到 14 字节: "T-demo-message"输出说明:第 [4] 步一次 epoll_wait 同时返回 pipe1、pipe2 两个就绪事件—— 这正是 lmkd 单线程处理多 fd 的核心机制;第 [8] 步 ET 模式下缓冲区有数据 却不通知,第 [9] 步改回 LT 立即通知,两种触发模式的差异一目了然。
要点总结
- epoll 三步走:
epoll_create建实例 →epoll_ctl(ADD/MOD/DEL)管理被监听的 fd →epoll_wait阻塞取就绪事件并逐个处理, lmkd 的 mainloop() 就是这个模型。 epoll_event.data是 64 位用户数据,内核原样带回:lmkd 存回调 函数指针实现免分支分发,一般程序存 fd 或连接对象指针。epoll_create的 size 参数现代内核已忽略(但必须 > 0);真正的 上限由epoll_wait的maxevents和 fd 注册数量决定。epoll_wait返回 -1 且 errno 为 EINTR 不是错误,是被信号打断, 像 lmkd 一样 continue 重试即可;timeout 传 -1 表示无限期阻塞。- 默认 LT 模式只要缓冲区有数据就反复通知,简单不易错(lmkd 的选择); ET 只在状态变化时通知一次,效率高但必须配合非阻塞 fd 并一次性读到 EAGAIN。