Parent-Child Processes - multithreading

What happens when the parent process of a child process exits before the child ?

Orphaned child processes all become the children of init, process 1.

Related

How to terminate a child when the parent process (not thread) dies

I have a parent and a child process where the child is
spawned (via clone) by a short lived thread of the parent and
lives in a PID namespace.
What I want to achieve is that the child process is terminated if the parent process is somehow terminated.
I understand from this answer that I can set the parent-death signal from the child like so:
prctl(PR_SET_PDEATHSIG, SIGHUP);
However, this already triggers when the parent's thread from which the child was spawned terminates. What I need is a mechanism that only triggers when the parent process terminates.
I tried using waitpid from the child to monitor the parent process but since it is spawned in a PID namespace, the real PID of the parent is not accessible.

How can I kill the parent process from a child thread spawned by cluster.fork?

An error gets thrown in a child thread created by cluster.fork() and I want to kill the parent (and in doing so, kill all other sibling threads.
How can I do this from within the child thread (cluster.isWorker === true:true)?
I think I figured it out. From within a worker, I can't seem to access process globally, but this seems to work:
cluster.worker.process.kill(cluster.worker.process.ppid)

Why the return value of fork() is not in the reverse way?

We know that the fork() creates a new child process and returns the child's PID to the parent and 0 to the child.What I was thinking that why they have designed it in this way (return value),why the return value is not in the reverse order.I think returning the parent's PID to child and 0 to the parent may also work,every child would know who is its parent and whenever it gets terminated it will signal to its respective parent process,what will the consequences of this approach? Am I missing something?
The reason it's this way is that any child can get the pid of its parent by calling getppid, while the parent won't know what the child's pid is without fork telling it.
If the parent wants to wait for a specific child to exit, it needs to pass the child's pid to waitpid.
The child process can get the parent's PID by getppid(). Therefore the parent needs fork to get the PID of the child process
If they designed like, fork() returns 0 to the parent process then in the case of one parent process is creating multiple child processes.
if multiple fork() creates a child process and all returns 0's to the same parent process. There is no way to identify the individual child processes and designed to return child process id to the parent.

What would cause a SIGTERM to not propagate to child processes?

I have a process on Linux that starts up 20 child processes via fork. When I kill the parent process, it will often kill all of the child processes, but sometimes it doesn't kill all of them, and I'm left with some orphaned processes. This isn't a race condition on startup, this is after the processes have been active for several minutes.
What sort of things could cause SIGTERM to not propagate to some child processes properly?
There is no automatic propagation of signals (SIGTERM or otherwise) to children in the process tree.
Inasmuch as killing a parent process can be observed to cause some children to exit, this is due to ancillary effects -- such as SIGPIPEs being caused when the child attempts to read or write to a pipeline with the dead parent on the other side.
If you want to ensure that children are cleaned up when your process receives a SIGTERM, install a signal handler and do it yourself.
If you use process group id (pgid) when sending a signal, the signal would be propagated to parent process and all its children.
To know pgid, use ps a -o pgid,command.

Difference between SIGKILL SIGTERM considering process tree

What is the difference between SIGTERM and SIGKILL when it comes to the process tree?
When a root thread receives SIGKILL does it get killed cleanly or does it leave it's child threads as zombies?
Is there any signal which can be sent to a root thread to cleanly exit by not leaving any zombie threads ?
Thanks.
If you kill the root process (parent process), this should make orphan children, not zombie children. orphan children are made when you kill a process's parent, and the kernel makes init the parent of orphans. init is supposed to wait until orphan dies, then use wait to clean it up.
Zombie children are created when a process (not its parent) ends and its parent does not take up its exit status from the process table.
It sounds to me like you are worried about leaving orphans because by definition, when you kill a zombies parent process, the zombie child itself dies.
To kill your orphans, use kill -9 , which is the equivalent SIGKILL.
Here is a more in depth tutorial for killing stuff on linux:
http://riccomini.name/posts/linux/2012-09-25-kill-subprocesses-linux-bash/
You can't control that by signal; only its parent process can control that, by calling waitpid() or setting signal handlers for SIGCHLD. See SIGCHLD and SA_NOCLDWAIT in the sigaction(2) manpage for details.
Also, what happens to child threads depends on the Linux kernel version. With 2.6's POSIX threads, killing the main thread should cause the other threads to exit cleanly. With 2.4 LinuxThreads, each thread is actually a separate process and SIGKILL doesn't give the root thread a chance to tell the others to shut down, whereas SIGTERM does.

Resources