title: How to create a thread for signal handling and exit the process upon receiving the signal?
body: <p>I written the below code to handle signals in separate thread to forcefully cleanup some resources and exit the complete process.</p>
  
  <p>Here is the brief note about the below code.</p>
  
  <ul>
  <li>When the signal is received,  set <strong>volatile sig_atomic_t sig_set_flag = 1;</strong> inside signal handler.</li>
  <li>In signal_handler_thread, checking sig_set_flag value in a loop.</li>
  <li><strong>if(sig_set_flag==1)</strong> send notifications like "i am going down" from <strong>signal_handler_thread</strong> and call <strong>exit(0);</strong> from the thread.</li>
  </ul>
  
  <p>Signals can be received by any thread in a process. So i am setting the global variable.</p>
  
  <p>I have 2 questions.</p>
  
  <p>1) This implementation is fine? or i have to block the signals for the main thread and handle only by the spawned thread ?</p>
  
  <p>2) How to block a signal to the main process and handle it in a thread?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;stdatomic.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdlib.h&gt;
  
  /*
   * Set this variable if any signal is received
   */
  volatile sig_atomic_t sig_set_flag = 0;
  
  pthread_mutex_t cleanup_mutex;
  
  /*
   * Resource cleanup function.
   */
  int cleaup_resources() {
  
      pthread_mutex_lock(&amp;cleanup_mutex);
      /*  
       * Send notification to all the clients.
       * Delete all the temp files
       */
      printf("Notified to clients.Exiting process\n");
      pthread_mutex_unlock(&amp;cleanup_mutex);
  
      return 0;
  }
  
  /*
   * Signal handler thread
   */
  void sig_term_handler(int sig_num) {
    sig_set_flag = sig_num;    
  }
  
  
  /*
   * Signal handler thread routine
   */
  void *signal_handler_thread(void * args) {
      while(1) {
           if(sig_set_flag != 0) {
               printf("%s : Signal flag is set for sig_no %d\n",__func__,sig_set_flag);
               cleaup_resources();
               break;
           }
           usleep(5);
      }
      exit(0);
  }
  
  int main()
  {
      int loop_count,status;
      pthread_t tid;
      pid_t pid;
      struct sigaction sig;
      sig.sa_handler = &amp;sig_term_handler;
      sig.sa_flags = 0;
      sigaction(SIGTERM, &amp;sig, NULL);
  
      /*
       * Spawn a thread to monitor signals.
       * If signal received, Exit the process.
       */
  
      pthread_create(&amp;tid, NULL, signal_handler_thread, NULL);
  
  
      while(1) {
       printf("Some time consuming task in progress... PID = %d\n",getpid());
       pid = fork();
       if(pid == 0) {
  
           sleep(100);
           return 0;
       } else {
       waitpid(pid, &amp;status, 0);
       loop_count++;
       if( loop_count&gt;=10)
          break;
       }
      }
      cleaup_resources();
      exit(0);
  
  }
  </code></pre>
  
  <p>Note:I know signals will interrupt the some system calls and EINTR will be set. Unfortunately some system calls (i.e) waitpid() will not be interrupted. So i spawned a thread to handle this scenario.</p>