int epoll_create(int size); int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
epoll_create:建立一個 epoll 句柄,size 參數在現代內核中已被忽略。建立後會占用一個 fd,用完務必 close。
epoll_ctl:註冊要監聽的事件類型。
epfd 是 epoll_create 的回傳值;op 有三種:EPOLL_CTL_ADD 註冊新 fd、EPOLL_CTL_MOD 修改已註冊的 fd、EPOLL_CTL_DEL 從 epfd 中移除;fd 是要監聽的描述符;event 指定監聽的事件。
struct epoll_event {
_uint32_t events;
epoll_data_t data;
}
typedef union epoll_data {
void *ptr;
int fd;
_uint32_t u32;
_uint64_t u64;
}
events 可用的集合:
EPOLLIN 可讀;EPOLLOUT 可寫;EPOLLPRI 有緊急事件可讀;EPOLLERR 發生錯誤;EPOLLHUP 被掛斷;EPOLLET 設為邊緣觸發;EPOLLLT 設為水平觸發;EPOLLONESHOT 只監聽一次。
epoll_wait:收集已發生的事件。events 是預先配置好的陣列,epoll 會把發生的事件填進去;maxevents 是陣列大小;timeout 為 NULL 表示一直阻塞直到有事件發生,為 0 則只檢測一次立刻回傳,特定值則是等待該毫秒數後逾時回傳。
epoll 工作在 ET 模式時必須使用非阻塞 socket,否則一個檔案句柄的阻塞讀寫就可能把處理多個描述符的任務餓死。
LT 模式下,一次收到多份資料仍會產生多個事件,且支援阻塞與非阻塞介面;只要緩衝區還有資料就會持續通知,錯誤率較低。
兩者的關鍵差異:新事件到來時,ET 能從 epoll_wait 取到,但若這次沒把該 socket 的緩衝區處理完,在沒有新事件的前提下就再也取不到資料;LT 則只要緩衝區裡還有東西,就一直能讀。
(1)使用記憶體映射(mmap),避免使用者空間到內核空間的複製。
(2)事先以 epoll_ctl 註冊 fd,一旦就緒,內核以類似 callback 的機制迅速激活它,進程在 epoll_wait 時就能收到通知。
(3)可監視的 fd 數量不受限制,上限就是系統能開啟的檔案數。
(4)I/O 效率不隨 fd 數量增加而下降。select/poll 需要自己不斷輪詢整個 fd 集合直到有裝置就緒,其間可能多次睡眠與喚醒交替;epoll 同樣會在 epoll_wait 中睡眠與喚醒,但裝置就緒時是以回呼函式把就緒 fd 放進就緒鏈表並喚醒進程。「醒著」的時候,select/poll 得遍歷整個 fd 集合,epoll 只需檢查就緒鏈表是否為空——省下的正是大量 CPU 時間,這就是回調機制帶來的效能提升。
epoll_server.c:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#define MAX_NUM 64
int startup(int port) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) { perror("socket"); exit(1); }
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_port = htons(port);
local.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *)&local, sizeof(local)) < 0) {
perror("bind"); exit(2);
}
if (listen(sock, 5) < 0) { perror("listen"); exit(3); }
return sock;
}
/* 設為非阻塞 */
void set_nonoblock(int _fd) {
int fl = fcntl(_fd, F_GETFL);
if (fl < 0) { perror("fcntl"); return; }
fcntl(_fd, fl | O_NONBLOCK);
}
int main() {
short port = 8080;
int listen_sock = startup(port);
struct sockaddr_in client;
socklen_t len = sizeof(client);
int epoll_fd = epoll_create(256);
int timeout = 1000;
if (epoll_fd < 0) { perror("epoll_create"); exit(1); }
struct epoll_event _ev; /* 要關注的 fd */
_ev.events = EPOLLIN;
_ev.data.fd = listen_sock;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_sock, &_ev) < 0) {
perror("epoll_ctl");
goto LABLE;
}
struct epoll_event _ev_out[MAX_NUM]; /* 已就緒的 fd */
char buf[1024 * 5];
memset(buf, '\0', sizeof(buf));
int ready_num = -1;
while (1) {
switch (ready_num = epoll_wait(epoll_fd, _ev_out, MAX_NUM, timeout)) {
case 0:
printf("timeout\n");
break;
case -1:
perror("epoll_wait");
break;
default: {
int i = 0;
for (; i < ready_num; i++) {
int _fd = _ev_out[i].data.fd;
if (_fd == listen_sock && (_ev_out[i].events & EPOLLIN)) {
int new_sock = accept(_fd, (struct sockaddr *)&client, &len);
if (new_sock < 0) { perror("accept"); continue; }
printf("get a new connect...\n");
set_nonoblock(new_sock);
_ev.events = EPOLLIN | EPOLLET;
_ev.data.fd = new_sock;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, new_sock, &_ev) < 0) {
perror("epoll_ctl");
close(new_sock);
continue;
}
continue;
}
if (_ev_out[i].events & EPOLLIN) {
printf("%s\n", buf);
}
}
}
break;
}
}
LABLE:
close(epoll_fd);
return 0;
}
Makefile:
epoll_server: epoll_server.c
gcc -o $@ $^
.PHONY: clean
clean:
rm -rf epoll_server