`
iunknown
  • 浏览: 405661 次
社区版块
存档分类
最新评论

epoll 摘要

阅读更多
http://blog.csdn.net/umbrella1984/archive/2006/10/06/1322890.aspx

epoll的使用

令人高兴的是,2.6内核的epoll比其2.5开发版本的/dev/epoll简洁了许多,所以,大部分情况下,强大的东西往往是简单的。唯一有点麻烦是epoll有2种工作方式:LT和ET。

LT(level triggered)是缺省的工作方式,并且同时支持block和no-block socket.在这种做法中,内核告诉你一个文件描述符是否就绪了,然后你可以对这个就绪的fd进行IO操作。如果你不作任何操作,内核还是会继续通知你 的,所以,这种模式编程出错误可能性要小一点。传统的select/poll都是这种模型的代表.

ET (edge-triggered)是高速工作方式,只支持no-block socket。在这种模式下,当描述符从未就绪变为就绪时,内核通过epoll告诉你。然后它会假设你知道文件描述符已经就绪,并且不会再为那个文件描述 符发送更多的就绪通知,直到你做了某些操作导致那个文件描述符不再为就绪状态了(比如,你在发送,接收或者接收请求,或者发送接收的数据少于一定量时导致 了一个EWOULDBLOCK 错误)。但是请注意,如果一直不对这个fd作IO操作(从而导致它再次变成未就绪),内核不会发送更多的通知(only once),不过在TCP协议中,ET模式的加速效用仍需要更多的benchmark确认。

http://blog.csdn.net/sodme/archive/2006/02/15/599132.aspx

那么,epoll和iocp到底又有什么不同呢?

以我目前粗浅的使用经验来看,至少可以得到以下结论:

1.iocp是在IO操作完成之后,才通过get函数返回这个完成通知的;而epoll则不是在IO操作完成之后才通知你,它的工作原理是,你如果想进行IO操作时,先向epoll查询是否可读或可写,如果处于可读或可写状态后,epoll会通过epoll_wait函数通知你,此时你再进行进一步的recv或send操作。

2. 在1的基础上,我们其实可以看到,epoll仅仅是一个异步事件的通知机制,其本身并不作任何的IO读写操作,它只负责告诉你是不是可以读或可以写了,而 具体的读写操作,还要应用层自己来作;但iocp的封装就要多一些,它不仅会有完成之后的事件通知,更重要的是,它同时封装了一部分的IO控制逻辑。从这 一点上来看,iocp的封装似乎更全面一点,但是,换个角度看,epoll仅提供这种机制也是非常好的,它保持了事件通知与IO操作之间彼此的独立性,使 得epoll的使用更加灵活。

http://www.kegel.com/c10k.html#strategies

Use nonblocking calls (e.g. write() on a socket set to O_NONBLOCK) to start I/O, and readiness notification (e.g. poll() or /dev/poll) to know when it's OK to start the next I/O on that channel. Generally only usable with network I/O, not disk I/O.

http://monkeymail.org/archives/libevent-users/2005-September/000012.html

>> As a workaround, you could possibly forward long-during synchronous
>> actions (in a nonblocking-way) to a pipe with a thread(-pool) behind it.
>> The thread(s) could "signal" the completion of the synchronous action
>> through another pipe that is registered within the libevent-context.
>> This way you require only a single lib-event-context while being able to
>> handle long-during synchronous actions.

>This is a pretty good idea. That way the event notification can come
>through the main libevent loop.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics