Linux 民意调查与 epoll 洞察力
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8858328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
poll vs. epoll insight
提问by Cartesius00
Are there some simple rules of thumb when to use poll
vs. epoll
in a low-latency environment? epoll
should have higher overhead if only few of file-descriptors is monitored. Please, give some insight, answers "check it yourself" put elsewhere.
在低延迟环境中何时使用poll
vs.是否有一些简单的经验法则epoll
?epoll
如果只监视很少的文件描述符,则应该有更高的开销。请给出一些见解,答案“自己检查”放在其他地方。
采纳答案by R.. GitHub STOP HELPING ICE
Always use poll
unless all of the following are satisfied:
poll
除非满足以下所有条件,否则始终使用:
- You can ensure you're on a (Linux) system that has
epoll
or you provide a fallback for systems that don't. - You have a hugenumber of file descriptors active (at least 1000-10000).
- The set of file descriptors you're working with is stable over a long time period (adding/removing file descriptors from the
epoll
list is just as expensive as apoll
operation, since it requires entering/leaving kernelspace).
- 您可以确保您使用的 (Linux) 系统具有
epoll
或为没有的系统提供后备。 - 您有大量的文件描述符处于活动状态(至少 1000-10000)。
- 您正在使用的文件描述符集在很长一段时间内是稳定的(从
epoll
列表中添加/删除文件描述符与poll
操作一样昂贵,因为它需要进入/离开内核空间)。
回答by Fred Foo
epoll(7)
summarizes it succinctly: epoll
"scales well to large numbers of watched file descriptors." However, poll
is a POSIX standard interface, so use that when portability is required.
epoll(7)
简洁地总结它:epoll
“可以很好地扩展到大量监视的文件描述符。” 但是,poll
是 POSIX 标准接口,因此在需要可移植性时使用它。
回答by plaes
First of all, poll(2)
is only level-triggered, but epoll(4)
can be used as either edge- or level-triggered interface.
首先,poll(2)
它只是电平触发,但epoll(4)
可以用作边缘或电平触发接口。
Now complexity: poll
complexity regarding number of watched descriptors (fds) is O(n) as it scans all the fds every time when a 'ready' event occurs, epoll
is basically O(1) as it doesn't do the linear scan over all the watched descriptors.
现在的复杂性:poll
关于观察描述符 (fds) 数量的复杂性是 O(n),因为它每次发生“就绪”事件时都会扫描所有 fds,epoll
基本上是 O(1),因为它不进行所有线性扫描监视的描述符。
In terms of portability - as epoll
is Linux-specific I would suggest checking out libevand libeventlibraries.
Also, check out Dan Kegel's excellent writeup: "The C10K problem".
在可移植性方面 - 与epoll
Linux 特定的一样,我建议查看libev和libevent库。另外,请查看 Dan Kegel 的优秀文章:“ C10K 问题”。