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

发布于 2022-08-28 11:57:55 字数 103 浏览 18 评论 0

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏别ゝ 2022-09-04 11:57:55

你这概念上是不是有个误点。子进程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

The Life Cycle of a Process

淡看悲欢离合 2022-09-04 11:57:55

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

沉溺在你眼里的海 2022-09-04 11:57:55

手工kill sigchild 信号到parent进程

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文