之前相关文章推荐:Redis高性能与epoll
本文,我们从源代码的角度,简单理解Redis是如何使用epoll以及epoll的实现原理。浅入浅出~ 找我交流
通过本文了解如下三件事儿,就算是达到了本文目的:
1、epoll是Linux提供的系统实现,核心方法只有三个
2、epoll效率高,是因为基于红黑树、双向链表、事件回调机制
3、redis的IO多路复用,Linux上用epoll进行了实现
epoll是Linux内核提供的一种多路复用器,照例问问Linux的男人:
EPOLL(7) Linux Programmer's Manual EPOLL(7)NAME epoll - I/O event notification facilitySYNOPSIS #include DESCRIPTION The epoll API performs a similar task to poll(2): monitoring multiple file descriptors to see if I/O is possible on any of them. The epoll API can be used either as an edge-triggered or a level-triggered interface and scales well to large numbers of watched file descriptors. The following system calls are provided to create and manage an epoll instance: * epoll_create(2) creates an epoll instance and returns a file descriptor referring to that instance. (The more recent epoll_create1(2) extends the functionality of epoll_create(2).) * Interest in particular file descriptors is then registered via epoll_ctl(2). The set of file descriptors currently registered on an epoll instance is sometimes called an epoll set. * epoll_wait(2) waits for I/O events, blocking the calling thread if no events are currently available.
核心方法
man告诉我们epoll的定义在sys/epoll.h中,查看核心函数有3个:(在线代码 elixir.bootlin.com/linux/v4.19… )
epoll_create
epoll_create(int size)
核心功能:
- 创建一个epoll文件描述符
- 创建eventpoll,其中包含红黑树cache和双向链表
参数size并不是限制了epoll所能监听的文件描述符最大个数,只是对内核初始分配内部数据结构的一个建议。在Linux 2.6.8后,size 参数被忽略,但是必须传一个比 0 大的数。
调用epoll_create后,会占用一个fd值。在Lin