Calling kill on a child process with SIGTERM terminates parent process, but calling it with SIGKILL keeps the parent alive
<p>This is a continuation of <a href="https://stackoverflow.com/questions/40474600/how-to-prevent-sigint-in-child-process-from-propagating-to-and-killing-parent-pr">How to prevent SIGINT in child process from propagating to and killing parent process?</a> </p>

<p>In the above question, I learned that <code>SIGINT</code> wasn't being bubbled up from child to parent, but rather, is issued to the entire foreground process group, meaning I needed to write a signal handler to prevent the parent from exiting when I hit <code>CTRL + C</code>.</p>

<p>I tried to implement this, but here's the problem. Regarding specifically the <code>kill</code> syscall I invoke to terminate the child, if I pass in <code>SIGKILL</code>, everything works as expected, but if I pass in <code>SIGTERM</code>, it also terminates the parent process, showing <code>Terminated: 15</code> in the shell prompt later.</p>

<p>Even though SIGKILL works, I want to use SIGTERM is because it seems just like a better idea in general from what I've read about it giving the process it's signaling to terminate a chance to clean itself up. </p>

<p>The below code is a stripped down example of what I came up with</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;signal.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;

pid_t CHILD = 0;
void handle_sigint(int s) {
  (void)s;
  if (CHILD != 0) {
    kill(CHILD, SIGTERM); // &lt;-- SIGKILL works, but SIGTERM kills parent
    CHILD = 0;
  }
}

int main() {
  // Set up signal handling
  char str[2];
  struct sigaction sa = {
    .sa_flags = SA_RESTART,
    .sa_handler = handle_sigint
  };
  sigaction(SIGINT, &amp;sa, NULL);

  for (;;) {
    printf("1) Open SQLite\n"
           "2) Quit\n"
           "-&gt; "
          );
    scanf("%1s", str);
    if (str[0] == '1') {
      CHILD = fork();
      if (CHILD == 0) {
        execlp("sqlite3", "sqlite3", NULL);
        printf("exec failed\n");
      } else {
        wait(NULL);
        printf("Hi\n");
      }
    } else if (str[0] == '2') {
      break;
    } else {
      printf("Invalid!\n");
    }
  }
}
</code></pre>

<p>My educated guess as to why this is happening would be something intercepts the SIGTERM, and kills the entire process group. Whereas, when I use SIGKILL, it can't intercept the signal so my kill call works as expected. That's just a stab in the dark though.</p>

<p>Could someone explain why this is happening?</p>

<p>As I side note, I'm not thrilled with my <code>handle_sigint</code> function. Is there a more standard way of killing an interactive child process?</p>