1. epoll_create
#include
int epoll_create(int size);
size参数现在并不起作用,只是给内核一个提示,告诉它事件表需要多大。
2. epoll_ctl
#include
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event);
fd参数是要操作的文件描述符。
event参数指定事件,这是epoll_event结构指针。
struct epoll_event
{
__uint32_t events; //epoll事件
epoll_data_t data; //用户数据
}
data成员有存储用户数据:
typedef union epoll_data
{
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;
3. epoll_wait
#include
int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
代码差异如下:
//索引poll返回的就绪文件描述符
int ret = poll(fds, MAX_EVENT_NUMBER, -1);
for (int i = 0; i < MAX_EVENT_NUMBER; i++) {
if (fds[i].revents & POLLIN) {
int sockfd = fds[i].fd;
//处理sockfd...
}
}
//索引epoll返回的就绪文件描述符
int ret = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1);
for (int i = 0; i < ret; i++) {
int sockfd = events[i].data.fd;
//处理sockfd...
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_EVENT_NUMBER 1024
#define BUFFER_SIZE 10
int setnonblocking(int fd);
void addfd(int epollfd, int fd, bool enable_et);
void lt(epoll_event *events, int number, int epollfd, int listenfd);
void et(epoll_event *events, int number, int epollfd, int listenfd);
/*
* 用telnet到这个服务端程序上,并一次传输超过10字节(BUFFER_SIZE的大小)的数据,
* 然后比较LT和ET的异同,会发现ET比LT下事件被触发的次数少很多。
*/
int main(int argc, char **argv)
{
if (argc != 3) {
fprintf(stderr, "Usage: %s ip port\n", basename(argv[0]));
return 1;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
int ret = 0;
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(port);
inet_pton(AF_INET, ip, &address.sin_addr);
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
assert(sockfd >= 0);
int reuse = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
ret = bind(sockfd, (struct sockaddr*)&address, sizeof(address));
assert(ret != -1);
ret = listen(sockfd, 5);
assert(ret != -1);
epoll_event events[MAX_EVENT_NUMBER];
int epollfd = epoll_create(5);
assert(epollfd != -1);
addfd(epollfd, sockfd, true);
while (1) {
int ret = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1);
if (ret < 0) {
fprintf(stderr, "epoll failed: %s\n", strerror(errno));
break;
}
//lt(events, ret, epollfd, sockfd); //LT模式
et(events, ret, epollfd, sockfd); //ET模式
}
close(sockfd);
return 0;
}
//设置非阻塞文件描述符
int setnonblocking(int fd)
{
int old_option = fcntl(fd, F_GETFL);
int new_option = old_option | O_NONBLOCK;
fcntl(fd, F_SETFL, new_option);
return old_option;
}
//将描述符fd的EPOLLIN注册到epollfd提示的epoll内核事件中,参数enable_et指定是否启用ET模式
void addfd(int epollfd, int fd, bool enable_et)
{
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN;
if (enable_et) {
event.events |= EPOLLET;
}
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
setnonblocking(fd);
}
//LT模式
void lt(epoll_event *events, int number, int epollfd, int listenfd)
{
char buf[BUFFER_SIZE];
for (int i = 0; i < number; i++) {
int sockfd = events[i].data.fd;
if (sockfd == listenfd) {
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(client_address);
int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
addfd(epollfd, connfd, false);
}
else if (events[i].events & EPOLLIN) { //只要socket读缓存中还有未读出的数据,就会被触发
printf("event trigger once\n");
memset(buf, '\0', BUFFER_SIZE);
int ret = recv(sockfd, buf, BUFFER_SIZE-1, 0);
if (ret <= 0) {
close(sockfd);
continue;
}
printf("get %d bytes of content: %s\n", ret, buf);
}
else {
printf("something else happened\n");
}
}
}
//ET模式
void et(epoll_event *events, int number, int epollfd, int listenfd)
{
char buf[BUFFER_SIZE];
for (int i = 0; i < number; i++) {
int sockfd = events[i].data.fd;
if (sockfd == listenfd) {
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(client_address);
int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
addfd(epollfd, connfd, true);
}
else if (events[i].events & EPOLLIN) {
printf("event trigger once\n");
int ret = 0;
//因为ET模式不会重复触发,所以我们要循环读取所有数据
while (1) {
memset(buf, '\0', BUFFER_SIZE);
ret = recv(sockfd, buf, BUFFER_SIZE-1, 0);
if (ret < 0) {
//对于非阻塞I/O,下面的条件成立时表示数据已全部读取完毕
if (errno == EAGAIN || errno == EWOULDBLOCK) {
printf("read later!\n");
break;
}
close(sockfd);
break;
}
else if (ret == 0) {
close(sockfd);
}
else {
printf("get %d bytes of content: %s\n", ret, buf);
}
}
}
else {
printf("something else happened\n");
}
}
}
参考:《linux高性能服务器编程》