epoll原理_读懂才会用 - 瞅瞅Redis的epoll模型

之前相关文章推荐: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)

核心功能:

  1. 创建一个epoll文件描述符
  2. 创建eventpoll,其中包含红黑树cache和双向链表

参数size并不是限制了epoll所能监听的文件描述符最大个数,只是对内核初始分配内部数据结构的一个建议。在Linux 2.6.8后,size 参数被忽略,但是必须传一个比 0 大的数。

调用epoll_create后,会占用一个fd值。在Lin

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值