0

I want to spawn child processes inside child processes, and make it so, that if a parent is terminated, then each of its children is terminated recursively.

I'm currently using Python as the programming language, but I'm looking for a programming language agnostic solution.

What confuses me is that every Process is configured as either "daemonic" or "non-daemonic", depending on the daemon argument of __init__(), which controls whether the child process is terminated or not, when the parent exits.

But, as the following documentation shows, if a Process is "daemonic", then it cannot create children processes, and so it's impossible to achive what I want, i.e. to terminate children recursively, when a parent exits?

https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process.daemon:

daemon
The process’s daemon flag, a Boolean value. This must be set before start() is called.

The initial value is inherited from the creating process.

When a process exits, it attempts to terminate all of its daemonic child processes.

Note that a daemonic process is not allowed to create child processes. Otherwise a daemonic process would leave its children orphaned if it gets terminated when its parent process exits. Additionally, these are not Unix daemons or services, they are normal processes that will be terminated (and not joined) if non-daemonic processes have exited.
3
  • I'm not a python programmer, but I suspect that that "daemonic" thing is something "pythonic", i.e. an arbitrary absurd limitation which should defend the poor code monkeys from themselves ;-) In Unix, if you want to group processes you use a process group, which allows you to kill all of them at once. You don't try to recurse through a "process tree" (which is a fiction, since any process can get rid its parent by a double fork). – user414777 Oct 2 '20 at 8:08
  • @user414777 - thank you. How can a double-fork get rid of the parent? Isn't the double-forked process still a descendant of the original parent? Why not? – Shuzheng Oct 2 '20 at 8:26
  • In a philosophical sense it's still a descendant, of course. If you trace all the process creations (with strace or forkstat), you can build the whole genealogy tree ;-) But when the parent of a process dies, the kernel re-parents it to init (or a subreaper), and from that point on that's the process which appears as its parent in ps, which can retrieve its exit status, will be signaled with SIGCHLD when its adopted child dies, etc. – user414777 Oct 2 '20 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.