复制 epoll 文件描述符
Duplicating epoll file descriptor
有没有办法复制使用 epoll_create
创建的文件描述符,以便可以独立修改副本(adding/removing 监视使用 epoll_ctl
的文件描述符)。
例如我创建了一个 epoll 文件描述符 A
,它等待文件 P
和 Q
上的事件。我将它复制到 epoll 文件描述符 B
,并使 B
也等待文件 R
上的事件。调用 epoll_wait(A)
仍然只会等待 P
和 Q
.
这是在 A
上调用 dup
时的行为,还是需要使用 epoll_create
和 epoll_ctl
重新创建 epoll 文件描述符?
这有点令人困惑。
当您打开一个新文件时,会创建两个实体。一个是内核中的文件句柄。另一个是文件描述符,一个引用该文件句柄的数字。
虽然我不确定 epoll fd 会发生什么,但我假设它与任何其他重复的 fd 相同,那就是它们是相同的文件句柄。
有关提示,epoll(2)
手册页的摘录可能有所帮助:
Q6 Will closing a file descriptor cause it to be removed from all epoll sets automatically?
A6 Yes, but be aware of the following point. A file descriptor is a reference to an open file description (see open(2)
).
Whenever a 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 epoll set only after all the file
descriptors referring to the underlying
open file description have been closed (or before if the descriptor is explicitly removed using epoll_ctl(2)
EPOLL_CTL_DEL
).
This means that even after a file
descriptor that is part of an epoll set has been closed, events may be reported for that file descriptor if other file
descriptors referring to the same underlying
file description remain open.
所以,虽然我自己没有检查过,但我猜测 dup
不允许您以任何方式复制 epoll 的过滤器列表。两个 fd
s 将引用相同的文件句柄。对一个过滤器所做的任何更改都会反映在另一个过滤器中。
不幸的是,由于我不知道 API 可以查询 epoll
的过滤器列表,这意味着您无法做您想做的事,除了跟踪从头开始。
您可以在不同的底层文件结构(或具有不同fd的相同文件结构)上独立操作重复的epfd。
有没有办法复制使用 epoll_create
创建的文件描述符,以便可以独立修改副本(adding/removing 监视使用 epoll_ctl
的文件描述符)。
例如我创建了一个 epoll 文件描述符 A
,它等待文件 P
和 Q
上的事件。我将它复制到 epoll 文件描述符 B
,并使 B
也等待文件 R
上的事件。调用 epoll_wait(A)
仍然只会等待 P
和 Q
.
这是在 A
上调用 dup
时的行为,还是需要使用 epoll_create
和 epoll_ctl
重新创建 epoll 文件描述符?
这有点令人困惑。
当您打开一个新文件时,会创建两个实体。一个是内核中的文件句柄。另一个是文件描述符,一个引用该文件句柄的数字。
虽然我不确定 epoll fd 会发生什么,但我假设它与任何其他重复的 fd 相同,那就是它们是相同的文件句柄。
有关提示,epoll(2)
手册页的摘录可能有所帮助:
Q6 Will closing a file descriptor cause it to be removed from all epoll sets automatically?
A6 Yes, but be aware of the following point. A file descriptor is a reference to an open file description (see
open(2)
). Whenever a descriptor is duplicated viadup(2)
,dup2(2)
,fcntl(2)
F_DUPFD
, orfork(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 epoll set only after all the file descriptors referring to the underlying open file description have been closed (or before if the descriptor is explicitly removed usingepoll_ctl(2)
EPOLL_CTL_DEL
).
This means that even after a file descriptor that is part of an epoll set has been closed, events may be reported for that file descriptor if other file descriptors referring to the same underlying file description remain open.
所以,虽然我自己没有检查过,但我猜测 dup
不允许您以任何方式复制 epoll 的过滤器列表。两个 fd
s 将引用相同的文件句柄。对一个过滤器所做的任何更改都会反映在另一个过滤器中。
不幸的是,由于我不知道 API 可以查询 epoll
的过滤器列表,这意味着您无法做您想做的事,除了跟踪从头开始。
您可以在不同的底层文件结构(或具有不同fd的相同文件结构)上独立操作重复的epfd。