The MSDN page on Job Objects explains:
A process can be associated with only one job. Jobs cannot be nested. The ability to nest jobs was added in Windows 8 Consumer Preview and Windows Server 8 Beta.
Unfortunately, it seems that this is just what I need. I'm dealing with a process tree like this:
server.exe
|
+--+ utility.exe
|
+--+ launcherA.exe
| |
| +--+ programA.exe
| |
| +--+ subProcessA.exe
|
+--+ launcherB.exe
|
+--+ programB.exe
I try to implement the following behaviour:
If
server.exegets terminated somehow (because it crashes, or because the user decides to terminate it using the task manager, or simply because it finishes executing), it takes down all processes beneath it. I use a job object for this.If
launcherA.exeorlaunchedB.exeterminate for some reason, they take down all processes beneath them. Unfortunately I cannot use job objects here since job objects don't nest.
As it is, I often manage to create 'dangling' processes by killing arbitrary processes in the above tree. I try to avoid leaving any stale processes behind, but all solutions I've come up with so far rely on some sort of watchdog process which monitors other processes - but if the watchdog itself gets killed, all hope is lost.
server.exewatchlauncherA.exeandlaunchedB.exe, I do notice when they get terminated for some reason. However - how can I figure out the child processes at that point? I'm usingWaitForSingleObjectto get notified about the process end, but at that time I cannot tell what the child processes are because they have already been reparented. I did considerCreateToolhelp32Snapshotto keep track of child processes, or maybe WMI. Is there maybe an easier solution? - Frerich Raabeserver.exeis the watchdog, I'd need to come up with a way to determine e.g. all processes beneathlauncherA.exeafterlauncherA.exeterminated. Taking process snapshots probably introduces race conditions, so the only solutions I can think of right now is hooking theNtCreateProcessAPI or using WMI to get notified of process creations. I wonder whether there's something else. - Frerich RaabeProcess32Firstto get the parent process ID. Feel free to write up your solution and accept it. - Raymond Chen