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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:04:10  来源:igfitidea点击:

poll vs. epoll insight

c++clinuxgcc

提问by Cartesius00

Are there some simple rules of thumb when to use pollvs. epollin a low-latency environment? epollshould have higher overhead if only few of file-descriptors is monitored. Please, give some insight, answers "check it yourself" put elsewhere.

在低延迟环境中何时使用pollvs.是否有一些简单的经验法则epollepoll如果只监视很少的文件描述符,则应该有更高的开销。请给出一些见解,答案“自己检查”放在其他地方。

采纳答案by R.. GitHub STOP HELPING ICE

Always use pollunless all of the following are satisfied:

poll除非满足以下所有条件,否则始终使用:

  1. You can ensure you're on a (Linux) system that has epollor you provide a fallback for systems that don't.
  2. You have a hugenumber of file descriptors active (at least 1000-10000).
  3. The set of file descriptors you're working with is stable over a long time period (adding/removing file descriptors from the epolllist is just as expensive as a polloperation, since it requires entering/leaving kernelspace).
  1. 您可以确保您使用的 (Linux) 系统具有epoll或为没有的系统提供后备。
  2. 您有大量的文件描述符处于活动状态(至少 1000-10000)。
  3. 您正在使用的文件描述符集在很长一段时间内是稳定的(从epoll列表中添加/删除文件描述符与poll操作一样昂贵,因为它需要进入/离开内核空间)。

回答by Fred Foo

epoll(7)summarizes it succinctly: epoll"scales well to large numbers of watched file descriptors." However, pollis 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: pollcomplexity regarding number of watched descriptors (fds) is O(n) as it scans all the fds every time when a 'ready' event occurs, epollis 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 epollis Linux-specific I would suggest checking out libevand libeventlibraries. Also, check out Dan Kegel's excellent writeup: "The C10K problem".

在可移植性方面 - 与epollLinux 特定的一样,我建议查看libevlibevent库。另外,请查看 Dan Kegel 的优秀文章:“ C10K 问题”。