io简化模型如下:
父进程
bind
listen
fork
waitpid
子进程
epoll_ctrl add
epoll_wait
accept
epoll 手册中说道,关闭fd将自动从epoll中删除
close of an fd cause it to be removed from all epoll sets automatically
坑爹的是,父子进程共享端口的情况下,监听句柄对应的struct file 文件结构,引用计数将为2,若只在子进程中关闭fd,而不显示调用epoll_ctl 将改句柄监听删除, file 结构引用计数将无法减为0,将无法释放改句柄在epoll_wait 中的epoll sets;由于file->socket->tcp_sock 关系仍然保持着,tcp监听端口还存在着,若此时有tcp请求连接时,将还会唤醒子进程的epoll_wait,而无法处理导致而陷入死循环;
对应内核代码:
fs/open.c close->filp_close->fput->__fput->eventpoll_release
fput 中有引用计数判断:
void fput(struct file *file)
{
if (atomic_long_dec_and_test(&file->f_count))
__fput(file);
}
void __fput(struct file *file)
{
******
fsnotify_close(file);
/*
* The function eventpoll_release() should be the first called
* in the file cleanup chain.
*/
eventpoll_release(file);