epoll如何工作的一般解释?
问题描述
我正在写一篇关于从数据库轮询(通过同步存储过程调用)切换到消息队列(通过发布/订阅)的技术文章.我希望能够解释轮询数据库与建立与 AMQP 代理的连接和配置消息处理程序相比有何巨大不同和重得多.
I'm doing a technical write-up on switching from a database-polling (via synchronous stored procedure call) to a message queue (via pub/sub). I'd like to be able to explain how polling a database is vastly different and much heavier than setting up a connection to a AMQP broker and configuring a message handler.
有人可以在这里提供一个解释,或者给我一个很好的高级教程,说明当通知套接字上有新数据可用时,epoll 是如何工作的?
Can someone maybe provide an explanation here, or point me to a good high level tutorial on how epoll works when notifying of new data becoming available on a socket?
解决方案
我假设epoll 的工作原理"是指从用户的角度来看它是如何工作的(例如,你的代码如何得到通知,以及应该处理它),而不是内核的观点(例如,如何实现 epoll).
I assume by "how epoll works" you're referring to how it works from the user point of view (as in, how your code gets notified, and should deal with it), as opposed to the kernel point of view (as in, how epoll is implemented).
简短版很简单:就像poll
,除了两点:
The short version is very simple: It's just like poll
, except for two things:
- 它使用不透明数据结构的句柄,因此您不会在内核边界来回传递太多数据.
- 它具有
poll
所没有的选项(尤其是边缘触发和一次性通知),可以让您在某些情况下编写更高效的代码.
- It uses a handle to an opaque data structure so you're not passing as much data back and forth across the kernel boundary.
- It has options that
poll
doesn't have (notably edge triggering and one-shot notifications) that can let you write more efficient code in certain situations.
(还有一个事实是它只能在 linux 上运行.BSD 和相关系统具有 kqueue
,这是获得相同优势的一种截然不同的方式,Solaris 具有 /dev/poll
等,有些 *nix 没有等价物.所以如果你想编写可移植的代码,你要么必须使用 poll
,要么使用一些更高级别的库,如 libevent
,或者自己编写libevent
的等价物.)
(There's also the fact that it only works on linux. BSD and related systems have kqueue
, a significantly different way to get some of the same advantages, Solaris has /dev/poll
, etc., and some *nixes have nothing equivalent. So if you want to write portable code, you either have to use poll
, or use some higher-level library like libevent
, or write the equivalent of libevent
yourself.)
如果您已经了解 select
和 poll
,那么 维基百科文章和博文 链接在其参考文献之间应该告诉你几乎所有你需要知道的东西,以及 手册页将填补任何空白.
If you already understand select
and poll
, the the Wikipedia article and the blog post linked in its References should, between them, tell you almost everything you need to know, and the man page will fill in any gaps.
如果没有,先去了解一下poll
,然后才知道epoll
有什么不同.
If not, go learn about poll
first, and only then will it make sense to learn how epoll
is different.
我仍然不确定这与您的主要问题有何关系.您可以在数据库文件上epoll
inotify
,或者在消息传递系统下使用pipe
或socket
,或者只是关于在 linux 中可以表示为文件描述符的任何其他内容,所以我不确定理解 epoll
将如何帮助您解释轮询数据库与轮询消息队列之间的区别.两者之间当然有很大的不同,但事件触发机制不是其中之一.
I'm still not sure how this relates to your main question at all. You can epoll
an inotify
on a database file, or a pipe
or socket
underlying a messaging system, or just about anything else that can be represented as a file descriptor in linux, so I'm not sure how understanding epoll
will help you explain the differences between polling a database vs. polling a message queue. There are of course vast differences between the two, but the event-triggering mechanism is not one of them.
相关文章