process re-parenting: controlling who is the new parent
Asked Answered
B

3

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.

Brothers answered 25/6, 2011 at 7:14 Comment(0)
S
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.

Smelly answered 25/6, 2011 at 7:21 Comment(0)
B
11

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.

Bonnette answered 6/8, 2013 at 20:26 Comment(0)
B
1

I think reptyr can perform what you want. Check it out:

https://linux.die.net/man/1/reptyr

https://github.com/nelhage/reptyr

Biannulate answered 13/7, 2022 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.