gpt4 book ai didi

c - epoll 是否保留 fd 的注册顺序?

转载 作者:IT王子 更新时间:2023-10-29 00:49:46 25 4
gpt4 key购买 nike

我正在研究 Linux 系统调用,我发现了 epoll 的某些方面,这对我来说不是很清楚。比如说,我创建了一个 epoll 实例:

epollfd = epoll_create(50);

接下来,我在 for 循环中注册了 50 个文件描述符:

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 ...

现在我们有 50 个文件,可以进行操作(读或写——无关紧要)。我们将 MAX_EVENTS 设置为 3:

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

所有这 50 个文件都已准备就绪,我们只要求其中的 3 个。哪些文件将在 events 数组中?

  • [1,2,3] -- 加入 epoll 的前 3 个文件
  • [48,49,50] -- 加入 epoll 的最后 3 个文件
  • [34, 7, 15] -- 随机3个文件
  • 任何其他选项

谢谢。

最佳答案

仔细阅读 epoll 的源文件,可以看到就绪事件在链表中维护。事件从列表的头部移除并添加到列表的末尾。

基于此,答案是描述符顺序基于它们准备就绪的顺序。

此行为现在记录在 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. ...

文档感谢 @mtk .

关于c - epoll 是否保留 fd 的注册顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19114001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com