SELECT/POLL存在的问题
Linux EPOLL是对select和poll的改进,因此在分析epoll之前需要对select/poll有一定的了解。
select存在的几个问题:
- 每次调用select,都需要把监控的fds集合从用户态拷贝到内核态,高并发场景下这样的拷贝会使得消耗的资源很大;
- 监听的端口数量有限;
- 有事件返回时,需要遍历fds集合,找到可读可写的事件;
poll对监听端口数量限制做了改进,但还是存在2个问题:
- 每次调用poll,都需要把监控的pollfds集合从用户态拷贝到内核态,高并发场景下这样的拷贝会使得消耗的资源很大;
- 有事件返回时,需要遍历pollfds集合,找到可读可写的事件;
什么是EPOLL
取维基百科上的一段话 (epoll
is a Linux kernelsystem call for a scalable I/O event notification mechanism, first introduced in version 2.5.44 of the Linux kernel.[1] Its function is to monitor multiple file descriptors to see whether I/O is possible on any of them. It is meant to replace the older POSIXselect(2)
andpoll(2)
system calls, to achieve better performance in more demanding applications, where the number of watched