C Reparenting a child process

Hi!

my situation is as follows: I have a process A with parent S (like in "shell"). Process A spawns a child B.

Is it possible for process A to re-parent process B to its own parent (i.e. process S), as if S had been the spawning process?

A possible solution (in the C programming language) does not need to be portable, FreeBSD-isms are welcome!

Best,
Holger
 
I'd say no, this is not possible. But I'd wait for someone smarter to join in.
EDIT: I mean by standard means from userspace, not hacking it away.
 
What problem does reparenting a process solve?
I am writing a small application which does nothing but launching ( execve's) another application (which the user can choose) and then exits. Since the child (i.e. the launched application) will be orphaned, its parent PID is set to 1 (the init process). I am not sure if that is desired behavior.
 
When a process exits, all its kernel managed resources are released, except for the proc table slot allocated to the process, where the process's exit status is stored.

The wait(2) system call does the job of undertaker. It collects the exit status, allowing the kernel to release the slot in the proc table for re-use.

Processes marked as Zombie, have exited, but have not been the subject of a wait system call.

In the normal run of things, the parent process should wait(2) for a child process to exit(2). However, if the parent exits before the child, without waiting for the child to exit, then such orphaned processes get inherited by init(8) (in the child's kernel metadata, PPID is set to 1).

Init will periodically wait for such children, thus acting as an uber-parent, and ensuring that every deceased process gets a proper burial.

It was ever thus. But with progress comes change. Funerary arrangements have become more complicated. See procctl(2).
 
Hi!

my situation is as follows: I have a process A with parent S (like in "shell"). Process A spawns a child B.

Is it possible for process A to re-parent process B to its own parent (i.e. process S), as if S had been the spawning process?

A possible solution (in the C programming language) does not need to be portable, FreeBSD-isms are welcome!

Best,
Holger
Yes and no, but perhaps not in the way you think.
You can achieve this by using procctl and PROC_REAP_ACQUIRE, however, you would have to code this into the shell.
You could write a program to reap the descendant children of the parent process but ultimately, everything goes back to init as the "reaper of last resort" and realistically, why bother? That parent has got to die sometime, even if it's only upon a reboot.

It's so much like a Shakespearean drama. ;)

I think your "desired behavior" is correct behaviour.
 
Back
Top