epoll

作者: 老杜振熙 | 来源:发表于2021-05-22 22:19 被阅读0次

Reference

The Implementation of epoll (1) (idndx.com)

Prerequisites

  • inode:可以理解为linux下每个文件的index number,其存储了关于这个文件的所有metadata,比如其读写权限,size,文件类型,以及文件内容存储的位置等等(Detailed Understanding of Linux Inodes)

FAQ

Q: 为什么epoll实体(instance)是用fd来进行引用呢?
A:因为这可以让epoll实体变得和socket一样可以poll

Q:epoll的核心是什么?
A:核心就是struct eventpoll这个内核数据结构,它定义并维护了epoll所需的所有操作;通过epoll_create()创建一个epoll实体,而在底层就对应一个struct eventpoll,一些细节如下:

  • 首先通过ep_alloc()创建一个struct eventpoll;
  • 然后通过fd = get_unused_fd_flags()获取一个系统当前还没有使用过的fd;
  • 然后通过struct file *anon_inode_getfile(const char *name, const struct file_operations *fops, void *priv, int flags)获取一个新的file;(priv参数对应的就是那个struct eventpoll实体)
  • 然后通过fd_install(fd, file)将fd和上面的file实体进行装载,从而让fd成为一个有效fd;
  • 最终,返回的这个fd就可以用来引用struct eventpoll实体

Q:epoll是如何记住用户注册的所有fd的?
A:通过struct eventpoll中的rbr成员,该成员是一个红黑树

相关文章

网友评论

      本文标题:epoll

      本文链接:https://www.haomeiwen.com/subject/jqmojltx.html