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

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

问题描述

我正在使用 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?

类似问题:

推荐答案

子进程与其父进程之间没有联系.它们可能知道彼此的进程 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天全站免登陆