Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does epoll preserve the order in which fd's was registered?

Tags:

c

linux

epoll

I'm playing around with Linux system call and I found some aspect of epoll, that is not clear to me. Say, I create a epoll instance:

epollfd = epoll_create(50);

Next, I register 50 file descriptors in for-loop:

for(i=0; i<50; i++){
    // open file "file-i".txt
    // construct epoll_event
    // register new file descriptor with epoll_ctl(epollfd, EPOLL_CTL_ADD ...

Now we have 50 file, that are ready for action(read or write -- doesn't matter). We set MAX_EVENTS to 3:

#define MAX_EVENTS 3
...
struct epoll_event events[MAX_EVENTS]
...
epoll_wait(epollfd, events, MAX_EVENTS, -1)

All of those 50 files were ready, we asked only for 3 of them. Which files will be in events array?

  • [1,2,3] -- first 3 files in order they were added to epoll
  • [48,49,50] -- last 3 files in order they were added to epoll
  • [34, 7, 15] -- random 3 files
  • any other option

Thank you.

like image 498
Artem Mezhenin Avatar asked Oct 01 '13 10:10

Artem Mezhenin


People also ask

What is the use of epoll?

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 epoll work in Linux?

epoll monitors I/O events for multiple file descriptors. epoll supports edge trigger (ET) or level trigger (LT), which waits for I/O events via epoll_wait and blocks the calling thread if no events are currently available. select and poll only support LT working mode, and the default working mode of epoll is LT mode.

Does epoll work on files?

Not really. epoll only makes sense for file descriptors which would normally exhibit blocking behavior on read/write, like pipes and sockets. Normal file descriptors will always either return a result or end-of-file more or less immediately, so epoll wouldn't do anything useful for them.

What is poll and epoll?

Both of them potentially trigger on a single event. However, with poll, the user then has no choice but to iterate thru the entire list submitted to find the events, whereas with epoll you get a list returned containing only the actual events. This means if the server is very busy, there is no advantage to epoll.


1 Answers

Perusing through the source file for epoll, one sees that the ready events are maintained in a linked list. Events are removed from the head of the list and added to the end of the list.

Based on that, the answer is that the descriptor order is based on the order in which they became ready.

This behavior is now documented in the notes for epoll_wait:

If more than maxevents file descriptors are ready when epoll_wait() is called, then successive epoll_wait() calls will round robin through the set of ready file descriptors. ...

The documentation is thanks to @mtk .

like image 152
jxh Avatar answered Sep 24 '22 17:09

jxh