The epoll set will be empty when you delete everything that has been added. As far as I know, you cannot understand the epoll set to find out if there are any file descriptors. Thus, it is up to you to decide when the set of eras becomes empty, as indicated in Armin's answer.
Since you have not explained what you expect from your program, I will assume that you expect it to exit when stdin is closed, because doing close(0) will potentially remove the file descriptor 0 from epoll to set. However, the code in the list is erroneous. If you continue to wait for an epoll set that does not contain file descriptors (deleted automatically or with EPOLL_CTL_DEL ), epoll_wait will always wait.
The following code shows this well.
#include <errno.h> #include <stdio.h> #include <sys/epoll.h> int main() { int epfd; int n; struct epoll_event ret; epfd = epoll_create(100); while((n = epoll_wait(epfd, &ret, 1, -1)) > 0) { /* Never gets here. */ printf("tick!\n"); } return 0; }
The epoll set does not contain file descriptors, so epoll_wait waits forever. If you had a file connected to stdin in your program, and no other file descriptor in your program was connected to stdin, close(0) would remove fd 0 from the set, the epoll set will become empty, and the next epoll_wait will wait forever.
In general, you manage the file descriptors in the epoll set yourself, do not rely on close calls to automatically remove the file descriptor from the set. It is up to you to decide whether to continue to wait for the epoll set after you close(0) .
I also suggest changing the structure of your program to epoll_wait after read . This ensures that you get data that could be received on stdin before the first call to epoll_wait .
Also, be careful with this code:
k=0; while((t=read(0, buf, 100)) > 0) { k+=t; } if(k == 0) { close(0); printf("stdin done\n"); }
If you assume that read in a loop returns 100 and then 0, indicating some data plus the end of the file, close(0) will not be called. The program will loop and hang on epoll_wait . It is best to check the result of each read specifically for the end of the file and errors.