epoll源码探秘(epoll_create)

epoll_create是epoll系列的核心函数,用于创建epoll句柄,并占用一个fd值。在Linux 2.6.8以后,size参数不再使用。函数内部包括ep_alloc()的调用,用于初始化epoll结构:文件等待队列、唤醒队列、就绪队列、红黑树等。此外,还会通过get_unused_fd_flags()获取文件描述符,anon_inode_getfile()创建匿名文件,最后fd_install()安装文件与fd的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

epoll源码探秘(epoll_create)

epoll系列的系统函数,很简单,但是很强大。epoll_create(),epoll_ctl() , epoll_wait(),三个就够了。

一些重要的结构:

/*
 * Each file descriptor added to the eventpoll interface will
 * have an entry of this type linked to the "rbr" RB tree.
 * Avoid increasing the size of this struct, there can be many thousands
 * of these on a server and we do not want this to take another cache line.(红黑树单节点)
 */
struct epitem {
	union {
		/* RB tree node links this structure to the eventpoll RB tree */
		struct rb_node rbn;
		/* Used to free the struct epitem */
		struct rcu_head rcu;
	};

	/* List header used to link this structure to the eventpoll ready list (列表头用于连接结构的eventpoll就绪列表)*/
	struct list_head rdllink;

	/*
	 * Works together "struct eventpoll"->ovflist in keeping the
	 * single linked chain of items.
	 */
	struct epitem *next;

	/* The file descriptor information this item refers to (关联的文件描述符)*/
	struct epoll_filefd ffd;

	/* Number of active wait queue attached to poll operations (轮询操作)*/
	int nwait;

	/* List containing poll wait queues */
	struct list_head pwqlist;

	/* The "container" of this item */
	struct eventpoll *ep;

	/* List header used to link this item to the "struct file" items list */
	struct list_head fllink;
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值