Assume the following code where "sock" is a handle to TCP socket that was previously registered with an epoll file descriptor designated by epfd.
epoll_ctl(epfd, EPOLL_CTL_DEL, sock, &ev); close(sock); Is it still necessary to call epoll_ctl if the socket is going to get subsequently closed anyway? Or does the socket get implicitly deregistered as a result of closing it?
epoll_ctl with EPOLL_CTL_ADD should be called once for each socket. And when epoll_wait is called, it will monitor all the sockets that are added and return only those FDs that have some events to consume.
epoll is a Linux kernel system call for a scalable I/O event notification mechanism, first introduced in version 2.5. 44 of the Linux kernel. Its function is to monitor multiple file descriptors to see whether I/O is possible on any of them.
From the 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 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 anepollset 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 anepollset has been closed, events may be reported for that file descriptor if other file descriptors referring to the same underlying file description remain open.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With