僵死进程是怎么被回收的?
父进程fork了子进程。父进程没有安装SIGCHLD信号处理。在子进程exit的时候,父进程并没有运行到wait,可能过了很久,父进程wait了,此时还能回收僵死的子进程么?是怎么回收的?
Answers
你这概念上是不是有个误点。子进程exit了,所谓的“terminated”,并不是说它就立即就消失了,它事实上就是直接变成zombie,然后等待parent process去reap它。如果parent一直没有调用wait或waitpid之类的方法就直接exit了,那这个zombie就会reparent到init进程。
你读
man wait
:
All of these system calls are used to wait for state changes in a child of the calling process,
and obtain information about the child whose state has changed. A state change is considered
to be: the child terminated; the child was stopped by a signal; or the child was resumed by a
signal. In the case of a terminated child, performing a wait allows the system to release the
resources associated with the child; if a wait is not performed, then the terminated child
remains in a "zombie" state (see NOTES below).
和
man exit
:
The exit() function causes normal process termination and the value of status & 0377 is
returned to the parent (see wait(2)).
我找了一张图《The Life Cycle of a Process》:
https://www.andrew.cmu.edu/course/15-440-s13/applications/ln/lecture2.html