简体   繁体   中英

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 SIGPIPE s 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM