epoll模型要点总结

 

(图是网上的,懒得自己画了)

1 epoll_ctl是向红黑树rbr插入、删除、修改fd。epoll_wait在双向链表rdllist中查询IO可读、可写、错误事件。 

为什么使用红黑树?从插入、删除考虑。

2 epoll_ctl插入新fd时,新建epitem会设置回调函数ep_poll_callback,把pwq->wait(等待队列)跟socket的等待队列关联起来。网卡有事件到来,直接插入到rdllist中,所以epoll不需要像select一样遍历事件。

3 epoll模型也需要把数据从内核态拷贝到用户态。

4 LT水平模型,默认的,数据没有读完,epoll_wait还能读到数据。ET边缘模式,只能在非阻塞模式下,使用者需要自己处理完所有可读数据。

zlm怎么做?

try {} catch (std::exception &ex) {}

5 几个结构体的关系

//eventpoll,epitem相互包括,
struct eventpoll { 
/** This mutex is used to ensure that files are not removed
	 * while epoll is using them. This is held during the event
	 * collection loop, the file cleanup path, the epoll file exit
	 * code and the ctl operations. */
struct mutex  mtx;
wait_queue_head_t wq; //重要变量—等待队列
/* Wait queue used by file->poll() */
wait_queue_head_t poll_wait;
struct  list_head  rdllist; //List of ready file descriptors
struct  rb_root  rbr; // RB tree root used to store monitored fd structs
rwlock_t  lock; //Lock which protects rdllist and ovflist //自旋锁:得不到锁不会引起进程休眠
struct epitem *ovflist;
struct file *file;
}


struct epitem {
	struct  rb_node  rbn;
	struct  list_head  rdllink; //跟eventpoll同变量
   /* List containing poll wait queues */
   struct eppoll_entry *pwqlist; //重要,其中结构体wait有回调函数
   struct  eventpoll *ep;
   struct epoll_event event;
}

struct eppoll_entry {
struct epitem *base; //有epitem
wait_queue_entry_t wait;
wait_queue_head_t *whead;
}
typedef struct wait_queue_entry wait_queue_entry_t;
struct wait_queue_entry {
	unsigned int		flags;
	void			*private;
	wait_queue_func_t	func; //ep_poll_callback
	struct list_head	entry;
};

6 epoll惊群(指在accept下):多个线程或多进程等待同一事件,比如accept,系统会唤醒所有线程,但只有一个线程得到该事件,导致效率低。设置epoll_event的events EPOLLEXCLUSIVE属性,可以解决该问题。

7 epmutex、ep->mtx、ep->lock、 三把锁的区别?

epmutex:用得少。This mutex is used to serialize ep_free() and eventpoll_release_file().

ep->mtx:保护结构体eventpoll的数据。

rwlock_t  lock; //Lock which protects rdllist and ovflist.

epoll_wait事件主要是可读事件,可写只回调一次。通过写日志知道的。

9 sock有poll函数,需要实现,如tcp_poll、udp_poll。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值