title: C++ WINAPI: How to kill child processes when the calling (parent) process is forcefully terminated?
body: <p>Could anyone kindly tell me how to kill child processes when the calling (parent) process is forcefully terminated? By the way, I am not able to change the source code of child application.</p>
  
  <p>I have checked the existing thread in StackOverflow and it seems JobObject is the correct way to do so. However when I tested it (using a console application to call notepad.exe), I found that when the console application exited, Notepad didn't.</p>
  
  <p>I used <code>CreateProcess</code> to spawn the new process.</p>
  
  <p>I have also seen somebody says that setting up a pipe between parent process and child process will do the job, but I have not yet tried it.</p>
  
  <p>If anyone can give me some hints, I would really appreciate it.</p>
  
  <p>Update: WINAPI <code>AssignProcessToJobObject</code> failed to work if without <code>| CREATE_BREAKAWAY_FROM_JOB</code> in <code>CreatProcess</code>. Now it works! </p>
  
  <p>Thank everyone.</p>
  
  <p>Update again:</p>
  
  <p>It's really tricky. I am always confused by Windows API.</p>
  
  <p>Group 1:</p>
  
  <p>Process flag:      <code>CREATE_SUSPENDED</code></p>
  
  <p>JobObject flag:    <code>JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_SECURITY_RESTRICTED_TOKEN or JOB_OBJECT_SECURITY_NO_ADMIN or JOB_OBJECT_LIMIT_BREAKAWAY_OK</code></p>
  
  <p>Result: <code>AssingProcessToJobObject</code> fails with Error code 5 Access is denied</p>
  
  <p>Group 2:</p>
  
  <p>Process flag:      <code>CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB</code></p>
  
  <p>JobObject flag:    <code>JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_SECURITY_RESTRICTED_TOKEN or JOB_OBJECT_SECURITY_NO_ADMIN</code></p>
  
  <p>Results: <code>AssingProcessToJobObject</code> succeeds, but child process fails to be killed when parent one is killed.</p>
  
  <p>Group 3:</p>
  
  <p>Process flag:      <code>CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB</code></p>
  
  <p>JobObject flag:    <code>JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE</code></p>
  
  <p>Results: <code>AssingProcessToJobObject</code> succeeds, and child process is automatically killed when parent one is killed.</p>
  
  <p>Group 4:</p>
  
  <p>Process flag:      <code>CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB</code></p>
  
  <p>JobObject flag:    <code>JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_BREAKAWAY_OK</code></p>
  
  <p>Results: same to Group 3.</p>
  
  <p>The following code uses JobObeject which I copied from <a href="http://cboard.cprogramming.com/windows-programming/60561-program-termination.html#post430075" rel="noreferrer">http://cboard.cprogramming.com/windows-programming/60561-program-termination.html#post430075</a></p>
  
  <pre><code>#define _WIN32_WINNT 0x0500
  #include &lt;windows.h&gt;
  
  int main(void)
  {
      HANDLE                               hJob;
      JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = { 0 };
      PROCESS_INFORMATION                  pi   = { 0 };
      STARTUPINFO                          si   = { 0 };
  
  
      /*
       * Create a job object.
       */
      hJob = CreateJobObject(NULL, NULL);
  
      /*
       * Causes all processes associated with the job to terminate when the
       * last handle to the job is closed.
       */
      jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
      SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &amp;jeli, sizeof(jeli));
  
      /*
       * Create the process suspended.
       */
      si.cb = sizeof(si);
      CreateProcess(TEXT("C:\\Windows\\System32\\Notepad.exe"), NULL, NULL, NULL, FALSE, 
                    CREATE_SUSPENDED | CREATE_BREAKAWAY_FROM_JOB /*Important*/, NULL, NULL, &amp;si, &amp;pi);
  
      /*
       * Add the process to our job object.
       */
      AssignProcessToJobObject(hJob, pi.hProcess); // Does not work if without CREATE_BREAKAWAY_FROM_JOB
  
  
      /*
       * Start our suspended process.
       */
      ResumeThread(pi.hThread);
  
      /*
       * At this point, if we are closed, windows will automatically clean up
       * by closing any handles we have open. When the handle to the job object
       * is closed, any processes belonging to the job will be terminated.
       * Note: Grandchild processes automatically become part of the job and
       * will also be terminated. This behaviour can be avoided by using the
       * JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK limit flag.
       */
  
      /*
       * Do what you like here. For demonstration purposes we will just wait
       * for the child process to complete. Click our close button to see
       * everything in action.
       */
      WaitForSingleObject(pi.hProcess, 3000);
      /*
       * Cleanup. As mentioned, Windows does this automagically when our process
       * exits, but it is good style to do it explicitly. 
       */
      CloseHandle(pi.hThread);
      CloseHandle(pi.hProcess);
  
      CloseHandle(hJob);
  
      return 0;
  }
  </code></pre>