Reputation: 61
1) I have epoll_wait(..., events, ...)
loop, do I need to reinitialize the events array before each iteration?
2) According to epoll()
manual examples there is no need, is it a mistake?
3) Does fds that I didn't handle yet will be re-written in the array next iteration? (I'm using level triggered epoll) I won't miss ready fds?
I have tried reading the kernel code to check if it overwrites the array each iteration or only adds to it, but with no success (if you can show me it will be great).
struct epoll_event ev, events[MAX_EVENTS];
...
for (;;) {
nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
...
}
Upvotes: 0
Views: 323
Reputation: 6154
epoll_wait
returns you the number of events
' elements it has written and you shouldn't care about the rest of the array. So I would say - no, you don't need to reinitialize this array or even initialize it as long as you always use the first nfds
elements.
To elaborate further: after each call to epoll_wait
you know for sure that it filled the first nfds
elements of the events
array, so you'll have to iterate through these elements to check what events occurred at which descriptors. However the rest of the elements in the events
array are basically garbage either from the previous epoll_wait
calls or from whatever memory region this array is allocated at, so all the elements with index >= nfds
don't contain any useful data.
Upvotes: 1