如何让父进程退出子进程? [英] How can I cause a child process to exit when the parent does?

查看:333
本文介绍了如何让父进程退出子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ProcessBuilder启动子进程,并且如果父进程执行,则需要子进程退出。在正常情况下,我的代码正在正确地阻止孩子。但是,如果我导致操作系统杀死父级,则子级将继续运行。

I am launching a child process with ProcessBuilder, and need the child process to exit if the parent process does. Under normal circumstances, my code is stopping the child properly. However, if I cause the OS to kill the parent, the child will continue running.

有没有办法将子进程绑定到父进程,这样当父母遇害时它会退出吗?

Is there any way to "tie" the child process to the parent, such that it'll exit when the parent is killed?

类似的问题:

  • How to make child process die after parent exits?
  • Are child processes created with fork() automatically killed when the parent is killed?

推荐答案

子进程与其父进程之间没有关系。他们可能知道彼此的进程ID,但它们之间没有硬连接。你在谈论孤儿进程。这是操作系统级别的问题。意味着任何解决方案都可能取决于平

There is no tie between a child process and its parent. They may know each others process ID, but there's no hard connection between them. What you're talking about a orphan process. And it's an OS level concern. Meaning any solution is probably platform dependent.

关于我唯一能想到的是让孩子定期检查其父母身份,如果父母关闭则退出。我认为这不会那么可靠。

About the only thing I can think of is to have the child check its parents status periodically, exiting if the parent's shutdown. I don't think this would be all that reliable though.

这篇关于如何让父进程退出子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆