How do I automatically destroy child processes in Windows? How do I automatically destroy child processes in Windows? windows windows

How do I automatically destroy child processes in Windows?


The Windows API supports objects called "Job Objects". The following code will create a "job" that is configured to shut down all processes when the main application ends (when its handles are cleaned up). This code should only be run once.:

HANDLE ghJob = CreateJobObject( NULL, NULL); // GLOBALif( ghJob == NULL){    ::MessageBox( 0, "Could not create job object", "TEST", MB_OK);}else{    JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };    // Configure all child processes associated with the job to terminate when the    jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;    if( 0 == SetInformationJobObject( ghJob, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli)))    {        ::MessageBox( 0, "Could not SetInformationJobObject", "TEST", MB_OK);    }}

Then when each child process is created, execute the following code to launch each child each process and add it to the job object:

STARTUPINFO info={sizeof(info)};PROCESS_INFORMATION processInfo;// Launch child process - example is notepad.exeif (::CreateProcess( NULL, "notepad.exe", NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)){    ::MessageBox( 0, "CreateProcess succeeded.", "TEST", MB_OK);    if(ghJob)    {        if(0 == AssignProcessToJobObject( ghJob, processInfo.hProcess))        {            ::MessageBox( 0, "Could not AssignProcessToObject", "TEST", MB_OK);        }    }    // Can we free handles now? Not sure about this.    //CloseHandle(processInfo.hProcess);     CloseHandle(processInfo.hThread);}

VISTA NOTE: See AssignProcessToJobObject always return "access denied" on Vista if you encounter access-denied issues with AssignProcessToObject() on vista.


One somewhat hackish solution would be for the parent process to attach to each child as a debugger (use DebugActiveProcess). When a debugger terminates all its debuggee processes are terminated as well.

A better solution (assuming you wrote the child processes as well) would be to have the child processes monitor the parent and exit if it goes away.


Windows Job Objects sounds like a good place to start. The name of the Job Object would have to be well-known, or passed to the children (or inherit the handle). The children would need to be notice when the parent dies, either through a failed IPC "heartbeat" or just WFMO/WFSO on the parent's process handle. At that point any child process could TermianteJobObject to bring down the whole group.