title: Catching SIGTERM in C
body: <p>I'm trying to learn how to catch sigterm for a larger assignment I have in class. I'm following the steps in this <a href="https://airtower.wordpress.com/2010/06/16/catch-sigterm-exit-gracefully/" rel="nofollow">tutorial</a> however it doesn't work for me. When I enter the command "kill [process id]" the sleep doesn't stop and just continues. I've tried kill both the child and parent IDs and nothing happens. Any ideas? Here's what I have:</p>
  
  <pre><code>#include &lt;signal.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  
  volatile sig_atomic_t done = 0;
  
  void term(int signum)
  {
     printf("Caught!\n");
     done = 1;
  }
  
  int main(int argc, char *argv[])
  {
      struct sigaction action;
      memset(&amp;action, 0, sizeof(action));
      action.sa_handler = term;
      sigaction(SIGTERM, &amp;action, NULL);
  
      int pid = getpid();
      int parentID = getppid();
      printf("PID is %d  and Parent is %d \n",pid, parentID);
  
      int loop = 0;
      while (!done)
      {
          printf("PID is %d  and Parent is %d \n",pid, parentID);
          int t = sleep(10);
          /* sleep returns the number of seconds left if
           * interrupted */
          while (t &gt; 0)
          {
              printf("Loop run was interrupted with %d "
                     "sec to go, finishing...\n", t);
              t = sleep(t);
          }
          printf("Finished loop run %d.\n", loop++);
      }
  
      printf("done.\n");
      return 0;
  }
  </code></pre>