Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep track of all descendant processes to cleanup?

I have a program that can fork() and exec() multiple processes in a chain. E.g.: process A --> fork, exec B --> fork, exec C --> fork, exec D. So A is the great-great-grandparent of C.

Now the problem is that I do not have any control of processes B, C and D. So, several things can happen.

  1. It might so happen that a descendant process can do setsid() to change its process group and session.
  2. Or one of the descendant process dies (say C) and hence its child (D) is parented by init.

Therefore, I can't rely on process group id or parent id to track all descendants of A. Is there any reliable way of keeping track of all descendants? More specifically, I would like to kill all the descendants (orphans and otherwise).

It would be also great if its POSIX compliant.

like image 841
vinodkone Avatar asked Aug 04 '11 06:08

vinodkone


1 Answers

The POSIX way to do this is simply to use process groups. Descendant processes that explicitly change their process group / session are making a deliberate decision not to have their lifetime tracked by their original parent - they are specifically emancipating themselves from the parent's control. Such processes are not orphans - they are adults that have "flown the nest" and wish to exert control over their own lifetime.

like image 131
caf Avatar answered Sep 21 '22 09:09

caf