介绍:
- 网上已经有很多关于Epoll讲解的教程和文章了,这里我介绍下我感觉不错的一篇:
https://blog.csdn.net/libaineu2004/article/details/70197825 - 本篇文章我主要想讲解一些网络上比较难查找到的一些问题,并给出自己的理解和补充,另外文后会附上个人认为比较应该注意的注意点和使用习惯。
疑问相关:
一、关于EOPLLIN和EPOLLOUT设置问题:
相信很多朋友在最初接触学习的时候,发现网上的很多示例代码在处理事件时候是类似如下处理:
这时候会产生三个疑问:
- 为什么不设置成:EPOLLIN|EPOLLOUT呢???这样岂是可以同时监听 “数据变为可读” 和 “数据变为可写” 了?
- 这么操作当从EPOLLIN换成EPOLLOUT时岂不是检测不到 “接收数据” 了?
- 什么时候设置成EPOLLOUT呢?
是不可以设置成EPOLLIN|EPOLLOUT的。有这样一段答案:
老王:“The way epoll works, is by hooking into the existing kernel poll subsystem. It hooks into the poll wakeups, via callback, and it that way it knows that "something" is changed. Then it reads the status of a file via f_op->poll() to know the status. What happens is that, if you listen for EPOLLIN|EPOLLOUT, when a packet arrives the callback hook is hit, and the file is put into a maybe-ready list. Maybe-ready because at the time of the callback, epoll has no clue of what happened. After that, via epoll_wait(), f_op->poll() is called to get the status of the file, and since POLLIN|POLLOUT is returned (and since you're listening for EPOLLIN|EPOLLOUT), that gets reported back to you. The POLLOUT event, by meaning a buffer-full->buffer-avail transition, did not really happen, but since POLLOUT is true, that gets reported back too.”
张三:“Ok, so make sure I understand you correctly, you're saying that currently the kernel doesn't have
awareness of the difference between EPOLLIN and EPOLLOUT events because at the time of the event, both EPOLLIN/EPOLLOUT are returned from the kernel and that at least for the near term that's not going to change. At some point, we can expect the EPOLLOUT to give the correct event, but not till later than .28.”
老王:“The kernel knows the difference between EPOLLIN and EPOLLOUT, of course. At the moment though, such condition is not reported during wakeups, and this is what is going to be changing.”
最主要的是这句:at the time of the callback, epoll has no clue of what happened。大概就是指的回调的时候,epoll是不能区分出是EPOLLIN还是EPOLLOUT的。换句话说如果你同时监听了EPOLLIN和EPOLLOUT,当发生了EPOLLIN,由于epoll无法区分种类,那么将会同时监听到EPOLLIN和EPOLLOUT事件(即使当时没有发生EPOLLOUT)。
快递到楼下菜鸟驿站, 菜鸟驿站给你发短信让你去取件,至于你手机开没开机,是不影响快递是否到达的。
什么时候设置为EPOLLOUT呢?
个人认为,当需要发送数据时,write的返回值。如果是-1错误是EAGAIN时候,这时候该记录下已经写了多少,fd设置监听EPOLLOUT即可,当发生EPOLLOUT时候表示可以继续写,直到发送完毕,然后再设置为EPOLLIN。
二、epoll_wait最后一个参数设置为几好些:
个人认为:
出现如下代码时候设置为-1 (多线程):
while(1)
{
n = epoll_wait(..., -1)
if (n== -1)
{
if (errno == EINTR)
continue;
}
}
出现如下代码时候设置为0:
void update()//多少毫秒一调用
{
n = epoll_wait(..., 0)
}
出现如下代码时候设置为某个数:
while(1)//多少毫秒一调用
{
time = 规定的刷新间隔时间 - 上次逻辑处理时间;//凑合看,不考虑小于0了
n = epoll_wait(..., time);
logic->update();
}
个人认为需要特殊的注意点:
- ET模式下,read和write一定要读完或者写完(网上很多示例)。
- 记得如果listen_fd设置的也是ET模式的话,记得全部accept完毕。
- 使用epoll_wait(..., -1)时候,记得如下处理
while(1)
{
n = epoll_wait(..., -1)
if (n== -1)
{
if (errno == EINTR)
continue;
}
}
- 当发生新用户连接时候:
connfd = ::accept4(listenfd, (struct sockaddr*)&saddr,&slen, SOCK_NONBLOCK | SOCK_CLOEXEC);
if (connfd == -1)
{
if (errno == EMFILE)
{
close(idlefd);
idlefd = accept(listenfd, NULL, NULL);
close(idlefd);
idlefd = open("/dev/null", O_RDONLY | O_CLOEXEC);
//todu
continue;
}
break;
}
当发生EMFILE时候,accept会返回-1,但是此时(经测试)你虽然调用过了accept,这个连接过来的用户是不会从accept列表中移除的。当你再次调用的时候,仍会常识为该用户分配一个新的fd值。如果此时关闭某个fd再次调用accept时,是可以为该用户分配到一个fd的(刚刚你关闭的那个)。这时候,你就可以很优雅的给该用户发送 “用户数目达到上限” 等消息,然后再做关闭等处理。
其他想不到了,随笔写个,也是我简书的第一篇文章。后期想到什么的话还会更新添加,先就这样吧!