为什么epoll比select效率更高(Why is epoll faster than select)?

Why is epoll faster than select?

A typical server might be dealing with, say, 200 connections. It will service every connection that needs to have data written or read and then it will need to wait until there's more work to do. While it's waiting, it needs to be interrupted if data is received on any of those 200 connections.

比如说一个典型的服务器需要处理200个连接。每个连接需要读或写数据,然后进入等待状态,直到被唤醒可以继续做下一步。

进程在等待状态时候,当这200个连接中任何一个接受到数据后,该进程的等待状态将会被打断。

 

With select, the kernel has to add the process to 200 wait lists, one for each connection. To do this, it needs a "thunk" to attach the process to the wait list. When the process finally does wake up, it needs to be removed from all 200 wait lists and all those thunks need to be freed.

对select来说,内核需要为把200个进程连接添加到对应的200个等待队列中。内核需要一个“thunk”来连接进程和等待队列。

当进程在等待队列中唤醒时候,需要把进程中等待队列中移除,该进程对应的“thunk”也需要释放掉。

 

By contrast, with epoll, the epoll socket itself has a wait list. The process needs to be put on only that one wait list using only one thunk. When the process wakes up, it needs to be removed from only one wait list and only one thunk needs to be freed.

作为对比,epoll自己有一个等待队列。所有的进程只需要一个等待队列,只要一个“thunk”.

当进程唤醒时候,该进程只会从那一个“thunk”中移除。总共只需要有一个“thunk”释放。

 

To be clear, with epoll, the epoll socket itself has to be attached to each of those 200 connections. But this is done once, for each connection, when it is accepted in the first place. And this is torn down once, for each connection, when it is removed. By contrast, each call to selectthat blocks must add the process to every wait queue for every socket being monitored.

要清楚,epoll socket会对这200连接创建连接,每个连接只创建一次。当每个连接移除时候,每个连接删除一次。

作为对照,每次调用select 都必须将进程添加到每个被监视的套接字的等待队列中。

 

Ironically, with select, the largest cost comes from checking if sockets that have had no activity have had any activity. With epoll, there is no need to check sockets that have had no activity because if they did have activity, they would have informed the epoll socket when that activity happened. In a sense, select polls each socket each time you call select to see if there's any activity while epoll rigs is so that the socket activity itself notifies the process.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值