Tuesday, September 6, 2011

How to kill a process tree on Windows

Option 1: Query the system to generate the process tree, then kill each process in the tree as noted below.


http://stackoverflow.com/questions/3235218/how-terminate-child-processes-when-parent-process-terminated-in-c/5680096#5680096

However, there is a corner case that if some of the processes in the tree invokes some other processes between the step collect process <=> create tree, then we will miss the newly created process.

Option 2: Use Job Objects mechanism provided by Windows

On the topmost parent, create a Job Object and assign the current process to it. This way, the later invoked descendant processes are automatically added into the Job Object. Then we can kill the Job Object to kill the entire tree.

http://msdn.microsoft.com/en-us/library/ms684161%28VS.85%29.aspx

This option avoid the corner case on Option 1, however, we need to program the code into the parent process in order to do this. 


Example of using Job Objects from C#

http://stackoverflow.com/questions/3342941/kill-child-process-when-parent-process-is-killed


No comments:

Post a Comment