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?
Thank you.
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.
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.
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.
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.
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 whenepoll_wait()
is called, then successiveepoll_wait()
calls will round robin through the set of ready file descriptors. ...
The documentation is thanks to @mtk .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With