unix - 进程重新父子关系 : controlling who is the new parent

标签 unix process fork orphan process-reaper

新父级是否总是“init”,或者是否有某种方法可以控制谁成为新父级

维基百科似乎表明它始终是“init”。我真心希望情况并非如此。我已经尝试了所有我能想到的 setpgid 和 setsid 方法,但没有运气。现在我看到这篇维基百科文章我需要建议。

In a Unix-like operating system any orphaned process will be immediately adopted by the special init system process. This operation is called re-parenting and occurs automatically. Even though technically the process has the "init" process as its parent, it is still called an orphan process since the process that originally created it no longer exists. Taken from wikipedia

我问这个问题的原因是因为我正在制作一个运行多个工作进程的 Mac 应用程序。我希望这些工作进程在 task manager 的进程层次结构中显示为主进程的子进程。 。一些工作进程以不同的用户身份运行,在 Mac OS X 上,我需要 fork 两次才能将权限传递给子进程。因为我“双叉”,工作人员当前作为守护进程运行,并且在使用任务管理器查看时,我看到工作人员将“init”作为其父进程。

最佳答案

孤儿总是被init收养。没有 Unix 方法可以将父进程更改为某个非 init 进程。

<小时/>

从 Linux 3.4 开始,这不再是严格的事实。仍然没有可移植的 Unix 方法来做到这一点,但正如 Andy Lutomirski 指出的那样,Linux 3.4 为 prctl 添加了 PR_SET_CHILD_SUBREAPER .

In effect, a subreaper fulfills the role of init(1) for its descendant processes.

关于unix - 进程重新父子关系 : controlling who is the new parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6476452/

相关文章:

c - 使用相对路径打开文件

unix - 子进程死亡时通知父进程

process - 在C#中启动非exe进程时可以使用模拟吗?

检查输入是否是有效的 shell 命令,Linux

c - 这段代码中创建了多少个进程?

mysql - 无法使用终端连接登录mysql数据库

linux - 在bash中将数字分成等份

perl - 从远程服务器获取文件 - Perl/Unix

android - 如何创建像 Facebook App、Gmail 或 Ymail 那样的后台进程

c - 等待子执行