title: Sending signal to a forked process that calls exec()
body: <p>I have a simple C program that forks a process and call exec to run a command as below:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;signal.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;sys/types.h&gt;
  
  int fork_process(int sleep_interval) {
     char cmd[30];
  
     pid_t pid = fork();       
     if (pid &gt; 0) {
         return pid;
     }
     else if (pid &lt; 0) {
          printf("At parent. Couldn't create a child process!\n");
          return pid;
     }
     else { 
          sprintf(cmd, "sleep %d; %s", sleep_interval, "gzip a &gt; a.gz");
          execlp("sh", "sh", "-c", cmd, (char *) 0);
     }
  }
  
  int main () {
     pid_t pid = fork_process(400);
  
     sleep (10);
     kill(pid, SIGTERM);
  
     return 1;
  }
  </code></pre>
  
  <p>When I run this program, I notice that <code>sh</code> internally forks a process to run <code>sleep 400</code>:</p>
  
  <pre><code>$ps x
    1428 pts/80   S+     0:00 ./kill_prog
    1429 pts/80   S+      0:00 sh -c sleep 400; gzip a &gt; a.gz
    1430 pts/80   S+      0:00 sleep 400
  </code></pre>
  
  <p>Now, when the <code>SIGTERM</code> signal is sent in the program to the child process through its pid (<code>1429</code> here), I notice that the child process terminates but not the process executing <code>sleep 400</code> (pid <code>1430</code>). In other words, the process executing <code>sleep 400</code> becomes a zombie until it completes. </p>
  
  <p>How do I send a kill signal such that the signal is propagated to processes forked within the child process? I tried using the process group id in <code>kill</code> as <code>kill(-1*pid, SIGTERM)</code> but to no avail. </p>