使用fork实现的多进程TCP服务器,在接收到多个客户端几乎同时发送的FIN后, 每个子进程发送ACK并exit, 产生一个SIGCHLD信号。多个SIGCHLD信号同时到达父进程的信号处理函数中。注意unix信号是没有排队一说的, 此时若在信号处理函数中循环wait,将产生未预期的结果,不能彻底关闭所有子进程从而产生僵尸进程。
其实所有进程关闭时都会变为僵尸进程,虽然关闭了,但是会留下进程描述符及其他数据结构。此时需要父进程调用waitpid对其进行释放。
We must specify the WNOHANG option: This tells waitpid not to block if there are running children that have not yet terminated. In Figure 5.7 , we cannot call wait in a loop, because there is no way to prevent wait from blocking if there are running children that have not yet terminated.