Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). 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.

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?

like image 752
bitcycle Avatar asked Dec 12 '12 17:12

bitcycle


People also ask

What is epoll used for?

epoll is a Linux kernel system call for a scalable I/O event notification mechanism, first introduced in version 2.5. 44 of the Linux kernel. Its function is to monitor multiple file descriptors to see whether I/O is possible on any of them.

How does a poll system call work?

The poll() feature allows programs to multiplex input and output through a series of file descriptors. In other words, the poll() system call is analogous to select() system call in working as it holds its fire for one of several file descriptors by becoming available for I/O.

What is an epoll event?

epoll stands for event poll and is a Linux specific construct. It allows for a process to monitor multiple file descriptors and get notifications when I/O is possible on them. It allows for both edge-triggered as well as level-triggered notifications.

What is poll and epoll?

Another difference is that select() modifies the user-supplied file descriptor bitmaps in place to signal which sockets are ready, while poll() has separate fields of input ("of interest") and output events per-socket. epoll is a Linux-specific enhancement of poll().


1 Answers

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).

The short version is very simple: It's just like poll, except for two things:

  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.

(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.)

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.

If not, go learn about poll first, and only then will it make sense to learn how epoll is different.

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.

like image 168
abarnert Avatar answered Nov 01 '22 05:11

abarnert