Issue
I am trying to play a bit with epoll
and there is a part that confuses me a bit. So, from the man pages of 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.
...
From my understanding, these examples are possible:
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)
My first question is if my understanding is correct.
The second part of my question refers to the way that Linux manages the silent delete from the epoll interest lists. So, when you call close(fd)
and fd is in one or more interest lists of epoll, how does the kernel know in which interest lists that fd? Is this data stored in the underlying file structure? I guess my question is how does the silent removal of fds from the epoll interest lists happen step by step.
Solution
When fd1 is removed and same is dup to fd2 then kernel does not remove open file description but does remove entry from file descriptor table i.e internal data struct in kernel still remain intact but not associated with fd1 index anymore
Internally epoll fd keeps interest list in kernel data structure where it understand what all open file description needs to be cleaned up when close for one of interest list is invoked. Following link does have more information. https://unix.stackexchange.com/questions/195057/what-is-an-open-file-description
Answered By - tej parkash