linux - 是否有必要在关闭之前从 epoll 中注销套接字?

标签 linux sockets epoll

假设以下代码,其中“sock”是 TCP 套接字的句柄,该套接字先前已使用 epfd 指定的 epoll 文件描述符注册。

epoll_ctl(epfd, EPOLL_CTL_DEL, sock, &ev);
close(sock);

如果套接字随后要关闭,是否仍然需要调用 epoll_ctl?或者套接字是否因关闭而隐式注销?

最佳答案

来自man page :

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.

关于linux - 是否有必要在关闭之前从 epoll 中注销套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8707601/

相关文章:

linux - 单声道 - 找不到命令

java - 当简单的 Java Nio 客户端发送消息时 Netty 服务器抛出异常

android - Android 1.5 中的服务器套接字编程,最省电的方式?

java - Linux 机器上 Netty 4.1 的性能调优

python - 从文件管理器打开多个文件

c - 何时在驱动程序代码中使用 stdint.h 的标量

linux - Grep 具有一定字符数的行,包括换行符

php - 如何在 PHP 和 C++ 之间建立通信?

linux - 为什么epoll_wait(),在5s~10s后返回,TCP连接超时

linux - epoll:指定EPOLLONESHOT时电平触发和边沿触发的区别