简体   繁体   English

我是否需要重新初始化epoll_wait的事件数组?

[英]Do I need to re-initialize epoll_wait's events array?

1) I have epoll_wait(..., events, ...) loop, do I need to reinitialize the events array before each iteration? 1)我有epoll_wait(..., events, ...)循环,是否需要在每次迭代之前重新初始化events数组?

2) According to epoll() manual examples there is no need, is it a mistake? 2)根据epoll()手册示例,这是没有必要的吗?

3) Does fds that I didn't handle yet will be re-written in the array next iteration? 3)我还没有处理过的fds会在下一次迭代时重新写入数组吗? (I'm using level triggered epoll) I won't miss ready fds? (我正在使用级别触发的epoll)我不会错过现成的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);
...
}

epoll_wait returns you the number of events ' elements it has written and you shouldn't care about the rest of the array. epoll_wait返回您已经写入的events数,并且您不必在意数组的其余部分。 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. 因此,我想说-不,您不需要重新初始化此数组,甚至不需要初始化它,只要您始终使用第一个nfds元素即可。

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. 进一步说明:在每次调用epoll_wait您肯定会知道它填充了events数组的前nfds元素,因此您必须遍历这些元素以检查在哪个描述符处发生了什么事件。 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. 但是, events数组中的其余元素基本上是来自先前epoll_wait调用或分配给该数组的任何内存区域中的垃圾,因此所有索引> = nfds的元素均不包含任何有用的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM