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.
Related
I know that as the parent process terminates, the daemon process also terminates.
But I need to do some finishing work, can I apply signal handling?
If possible, what type of signal should it map to?
Also, is the signal generated by the child process and the signal caused by the parent's termination distinguished?
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)
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.
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.
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.