防止子进程成为孤儿进程
我的 Linux 进程有 4 个子进程。经过一段时间的执行后,所有子进程都被 init 进程采用。我们该如何预防这种情况呢? (僵尸进程不是这种情况)。
该过程是用C语言编写的,操作系统是Linux。我的代码调用 waitpid!可能是什么问题? 99.99%我们不会遇到这个问题。
最后更新:如果有人执行“kill -9”怎么办?这会立即终止父进程并使子进程成为孤儿。
My Linux process has 4 children. After some execution time all children adopted by the init process. How do we prevent this situation? (this is not the case with Zombie processes).
The process is written in C and the OS is Linux. My code calls waitpid! What might be the problem? In 99,99% we don't have this problem.
Last update: What if someone executes "kill -9 "? This terminates immediately the parent process and leaves the children orphan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的进程被
init
重新设置父进程,则意味着它们的父进程已死亡。当进程的父进程死亡时,init
会采用它,以便在子进程(即init< /code>) 收到
SIGCHLD
。如果您不希望
init
成为您子进程的父进程,则必须确保您的进程一直存在,直到您的所有子进程都已死亡并被您的程序收割。If your processes are being reparented by
init
, that means that their parent process has died. When a process' parent dies,init
adopts it so that it can reap the zombie bywait()
ing on the child when it (that is,init
) receivesSIGCHLD
.If you do not want
init
to become the parent of your children, you will have to ensure that your process lives until all of your children have died and been reaped by your program.等孩子们出去后再自己出去。有关更多详细信息,请参阅
wait(2)
手册页。Wait for the children to exit before exiting yourself. See the
wait(2)
man page for more details.从主页检查您的 waitpid API 参数,并确保您的父进程不应在所有子进程完成之前结束。
你可以在这里发布你的代码吗?
Check from main page for your waitpid API parameters, and make sure your parent process should not be over before all child processes are finished.
Can you post your code here?