【发布时间】:2021-10-08 14:22:03
【问题描述】:
我正在尝试与epoll 一起玩,其中有一部分让我有点困惑。因此,来自 epoll 的手册页:
6. Will closing a file descriptor cause it to be removed from
all epoll interest lists?
Yes, but be aware of the following point. A file descriptor
is a reference to an open file description (see open(2)).
Whenever a file descriptor is duplicated via dup(2), dup2(2),
fcntl(2) F_DUPFD, or fork(2), a new file descriptor referring
to the same open file description is created. An open file
description continues to exist until all file descriptors
referring to it have been closed.
A file descriptor is removed from an interest list only after
all the file descriptors referring to the underlying open
file description have been closed.
...
根据我的理解,这些例子是可能的:
Example1:
fd1 added to the interest list
dup(fd1) -> fd2
close(fd1) - not removed from interest list because the underlying file is still open
event for the file -> epoll signals fd1
Example2:
fd1 added to the interest list
dup(fd1) -> fd2
close(fd1) - not removed from interest list because the underlying file is still open
another file open -> this gets fd1
event for the first file -> epoll signals fd1 (for me this looks like it should not happen)
我的第一个问题是我的理解是否正确。
我的问题的第二部分是指 Linux 管理从 epoll 兴趣列表中静默删除的方式。那么,当你调用close(fd) 并且fd 在epoll 的一个或多个兴趣列表中时,内核如何知道该fd 在哪些兴趣列表中呢?这些数据是否存储在底层文件结构中?我想我的问题是从 epoll 兴趣列表中静默删除 fds 是如何一步一步发生的。
【问题讨论】:
-
对于第一部分,事件数据是任何用户空间设置的,不一定需要解释为文件描述符。
-
对于第二部分,
struct file通过struct file的f_ep成员和@ 链接到struct epitems 列表(在“fs/eventpoll.c”中定义) 987654329@ 的fllink成员。当最后一个对struct file的引用被关闭时,eventpoll_release_file被调用(通过eventpoll_release)从其包含的兴趣列表中删除(ep_remove)列表中的每个struct epitem。 -
对于第二部分,你是对的。对于第一部分,我不确定我是否理解您的回答。
-
对于第一部分,我的意思是当
epoll_wait()填写struct epoll_event时,data成员(epoll_data_t类型)与用户代码在添加时设置的值相同将事件添加到兴趣列表(epoll_ctl()和EPOLL_CTL_ADD或EPOLL_CTL_MOD)。data成员的值需要是用户代码用来识别特定事件的值,但它不需要是文件描述符。
标签: linux linux-kernel operating-system polling epoll