Question

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.

Was it helpful?

Solution

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 .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top