gpt4 book ai didi

c - 复制 epoll 文件描述符

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:22 25 4
gpt4 key购买 nike

有没有一种方法可以复制使用 epoll_create 创建的文件描述符,以便可以修改副本(使用 epoll_ctl 添加/删除监视的文件描述符)独立。

例如我创建了一个 epoll 文件描述符 A,它等待文件 PQ 上的事件。我将它复制到 epoll 文件描述符 B,并使 B 也等待文件 R 上的事件。调用 epoll_wait(A) 仍然只会等待 PQ

这是在 A 上调用 dup 时的行为,还是需要使用 epoll_create 重新创建 epoll 文件描述符epoll_ctl?

最佳答案

这有点令人困惑。

当您打开一个新文件时,会创建两个实体。一个是内核中的文件句柄。另一个是文件描述符,一个引用该文件句柄的数字。

虽然我不确定 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 将引用相同的文件句柄。对一个过滤器所做的任何更改都会反射(reflect)在另一个过滤器中。

不幸的是,由于我知道没有 API 可以查询 epoll 的过滤器列表,这意味着你无法做你想做的事,除了从头开始跟踪.

关于c - 复制 epoll 文件描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37787619/

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