How to keep track of all descendant processes to cleanup?

Viewed 1513

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.

2 Answers
Related