首页 > 僵死进程是怎么被回收的?

僵死进程是怎么被回收的?

父进程fork了子进程。父进程没有安装SIGCHLD信号处理。在子进程exit的时候,父进程并没有运行到wait,可能过了很久,父进程wait了,此时还能回收僵死的子进程么?是怎么回收的?


你这概念上是不是有个误点。子进程exit了,所谓的“terminated”,并不是说它就立即就消失了,它事实上就是直接变成zombie,然后等待parent process去reap它。如果parent一直没有调用wait或waitpid之类的方法就直接exit了,那这个zombie就会reparent到init进程。
你读man 3 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 3 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


子进程挂了就等着父进程来给它收尸。如果父进程不给它收尸就退出了,那么交给init来收尸。
其实进程退出后其相关的内存等资源已经释放了,只剩下一个task struct结构在进程表中躺着吓唬人。

【热门文章】
【热门文章】