8

Is the new parent always "init" or is there some way to control who gets to be the new parent?

Wikipedia seems indicates that it's always "init". I really hope that this is not the case. I have tried everything I can think of with setpgid and setsid, but no luck. And now that I see this wikipedia article I need advice.

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

The reason I'm asking is because I'm making a Mac app that runs a number of worker processes. I want these worker processes to appear as children of the main process in the process-hierarchy of the task manager. Some of the workers run as different users and on Mac OS X I need to fork twice to pass privileges to the child process. Because I "double fork" the workers currently run as deamons, and when looking with task manager I see the workers are having "init" as their parent process.

4

2 回答 2

12

Orphaned children are always adopted by init. There is no Unix way of changing the parent to some non-init process.


As of Linux 3.4 this is no longer strictly true. There's still no portable Unix way of doing this but as Andy Lutomirski points out Linux 3.4 adds PR_SET_CHILD_SUBREAPER for prctl.

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

于 2011-06-25T07:21:43.273 回答
10

On Linux, you can use PR_SET_CHILD_SUBREAPER to indicate that your orphaned descendants should be re-parented to you instead of to init.

于 2013-08-06T20:26:50.110 回答