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?
Related
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.
The docs only say that Popen.kill() sends SIGKILL to the child.
Will the parent process wait until the child is killed or it just proceeds event if the child hasn't been killed?
How can I make sure that the child is killed?
https://docs.python.org/3/library/subprocess.html#subprocess.Popen.kill
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 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.
how to kill a child process on sending an interrupt with key stroke and then sending a signal to parent that the child process has been killed?
The parent process generally receives a signal whenever a child dies regardless. This is the SIGCHLD signal.
This signal can be blocked by the parent process, but otherwise it's always delivered when a child exits for whatever reason. The parent can tell why the child exited by using one of the wait family (aka wait, wait3, wait4, or waitpid) to harvest the return code.
It may be a challenge to get the child process to have focus, so that it gets keyboard events.
You can always use the kill command to send a signal to a given process. Don't let the name kill mislead you, you can send any signal with kill, kill -SIGNUM pid, but SIGTERM is the default, which will usually cause a process to exit.
So if you wanted to send a SEGV signal to process 11 you'd do
kill -SEGV 11
You could set up the parent to catch a signal, re-send it to the child via a call to kill(2), and then wait for the child to exit using waitpid.
The parent's signal handler would like something like so:
int status;
kill(child_pid, SIG_TO_SEND);/*send to child*/
waitpid(child_pid, &status);/* wait for child to give up */
If you were serious about keeping the parent up and knew the signal was only for the child you'd probably set a variable in the parent, get out of the signal handler, and then do the work of sending and waiting for the child. In general it's good to get out of signal handlers as quickly as you can.