title: Windows console signal handling for subprocess c++
body: <p>Suppose we have small program, written in C++, which looks like below.<br>
  This program itself <strong>intentionally</strong> does NOT perform signal handling via WinAPI call <code>SetConsoleCtrlHandler</code> - it's important part of question.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main() {
    while(true) {
      int status = system("EXTERNAL COMMAND");
      printf("RESULT STATUS = %d\n", status);
    }
  }
  </code></pre>
  
  <p>When <code>Ctrl+C</code> key combination have been pressed in terminal, program above have quite different behavior depend on which <code>"EXTERNAL COMMAND"</code> had been invoked</p>
  
  <p>1) If external command is <code>pause</code>, program will stand in infinite loop, invoking <code>pause</code> command step by step, and will print <code>"RESULT STATUS = 0" many times</code>, while not terminated forced via process kill.<br>
  2) If external command in <code>choice</code>, program will terminate immediately after <code>Ctrl+C</code> pressure. It will not print anything and does not return from <code>system</code> call.<br>
  3) If external command is <code>set /P VAR=</code>, program has much interesting behavior. When <code>Ctrl+C</code> pressed, program prints `"RESULT STATUS = 1" and continue working until first async call is performed.</p>
  
  <p>First and second case can be explained in following manner. Terminal windows is proxt between user input and target program, so when user presses <code>Ctrl+C</code>, terminal window perform dispatch signal itself to target process.<br>
  So some subprocesses can take manually terminal handler via <code>hConsole = GetStdHandle(STD_OUTPUT_HANDLE)</code> and perform own signal handling. Another subprocess does not do this, so signal passed into parent process and terminate it.</p>
  
  <p>But third case causes big question. If child process intercepts <code>SIGINT</code>, why parent process perform terminating after first async call. If not, why it does not terminate immediately and why and how it prints `"RESULT STATUS = 1" and continue working.</p>
  
  <p>Thanks</p>