简体   繁体   English

关于epoll工作原理的一般说明?

[英]General explanation of how epoll works?

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). 我正在做有关从数据库轮询(通过同步存储过程调用)切换到消息队列(通过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. 我想解释一下,轮询数据库与建立与AMQP代理的连接和配置消息处理程序相比,有很大的不同,而且要重得多。

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的工作原理的高级教程?

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). 我假设“ epoll的工作方式”是指从用户的角度来看它的工作方式(例如,如何通知您的代码并应对其进行处理),而不是内核的观点(例如,如何实施epoll)。

The short version is very simple: It's just like poll , except for two things: 简短的版本非常简单:它类似于poll ,但有两点:

  1. It uses a handle to an opaque data structure so you're not passing as much data back and forth across the kernel boundary. 它使用一个不透明数据结构的句柄,因此您不会在内核边界之间来回传递太多数据。
  2. 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. 它具有poll没有的选项(特别是边沿触发和单发通知),可以让您在某些情况下编写更有效的代码。

(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.) (还有一个事实,即它仅适用于linux。BSD和相关系统具有kqueue ,这是获得某些相同优势的明显不同的方式,Solaris具有/dev/poll等,而某些* nixes则没有任何等效项。因此如果您想编写可移植的代码,则必须使用poll ,或使用一些更高级别的库(如libevent ,或自己编写与libevent等效的库。)

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. 如果您已经了解selectpoll ,那么Wikipedia文章和其References中链接的博客文章应该在它们之间告诉您几乎所有您需要知道的内容,并且手册页将填补所有空白。

If not, go learn about poll first, and only then will it make sense to learn how epoll is different. 如果不是,请先了解poll ,然后才有必要了解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. 您可以在数据库文件,消息系统下的pipesocket或在Linux中几乎可以表示为文件描述符的任何其他东西上对inotify进行epoll ,因此我不确定了解epoll将如何帮助您解释轮询数据库与轮询消息队列之间的区别。 There are of course vast differences between the two, but the event-triggering mechanism is not one of them. 两者之间当然有很大的差异,但是事件触发机制并不是其中之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM