epoll源码分析(一)

本文深入剖析Linux 2.6内核的eventpoll.c,重点讲解epoll_create()函数的实现过程,包括数据结构初始化、ep_alloc()函数的作用、匿名文件描述符的创建以及系统调用的相关细节。文章旨在帮助读者理解epoll在内核中的工作原理。
摘要由CSDN通过智能技术生成

epoll源码分析(一)

主要数据结构

这里讨论的是linux2.6的eventpoll.c文件里面的代码, 所以讲的代码基本都是这个文件的, 其他个别不在里面的代码我已经写出了路径.

这里写的代码后面的描述会用到的数据结构和函数, 这里可以先跳过函数, 只了解一下重要结构体的元素就行, 后面讲解的时侯再回头看.

struct eventpoll
{
   
    spinlock_t lock;	// 自旋锁
    struct mutex mtx;	// 防止使用时被删除
    wait_queue_head_t wq;	// sys_epoll_wait() 使用的等待队列
    wait_queue_head_t poll_wait;	// file->poll() 使用的等待队列
    struct list_head rdllist;	// 事件满足条件的链表, 事件就绪, 准备在epoll_wait时写入用户空间
    struct rb_root rbr;	
    struct epitem *ovflist;	// 将事件到达的fd进行连接, 并发送至用户空间
    struct user_struct *user;
};
struct epitem
{
   
	struct rb_node rbn;
    struct list_head rdllink;	// 事件的就绪队列
    struct epoll_filed ffd;
    int nwait;
    struct list_ead pwqlist;	// poll 等待队列
    struct eventpoll *ep;		// 属于哪个结构体
    struct list_head fllink;	// 链接fd对应的file链表
    struct epoll_event event;	// 事件
};
struct epoll_filefd
{
   
    struct file *file;
    int fd;
};
static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
{
   
    next->prev = new;
    new->prev = prev;
    new->next = next;
    prev->next = new;
}

static inline void list_add_tail
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值