Are you writing code for other processes or is it something you cannot change?
If possible, I would consider modifying them so that they receive some kind of message (even through standard streams), so that they beautifully stop on request, completing the children, if any, on their own.
I do not think that the "process of destruction" is pure.
source share