title: Main doing different tasks on different signals
body: <p>So I'm working on a project that I need to have my main do different tasks when it receives a, let's say, <code>SIGINT</code> signal and other tasks when it receives a <code>SIGTSTP</code>. The rest of the time, main doesn't need to do anything. The code I have come up with until now has a signal handler function for all signals and based on <code>signo</code>, it sets the appropriate flag for that signal. Main runs in a <code>while(1)</code> loop (with some sleep calls to try to avoid busy waiting) and checks these flags. Here is the code: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  
  // Using exponential backoff for sleep time
  #define MAX_BACKOFF 3600    // 1 hour
  #define INITIAL_BACKOFF 1   // 1 sec
  
  static int backoff = INITIAL_BACKOFF;
  
  static int flgsigint = 0;
  static int flgsigtstp = 0;
  
  
  void sighandler(int signo)
  {
      if (signo == SIGINT)
          flgsigint = 1;
      else if (signo == SIGTSTP)
          flgsigtstp = 1;
      backoff = INITIAL_BACKOFF;
  }
  
  
  int main()
  {
      // Handler for SIGINT, SIGTSTP
      static struct sigaction act1;
      act1.sa_handler = sighandler;
      sigemptyset(&amp;(act1.sa_mask));
      sigaddset(&amp;(act1.sa_mask), SIGINT);
      sigaddset(&amp;(act1.sa_mask), SIGTSTP);
      sigaction(SIGINT, &amp;act1, NULL);
      sigaction(SIGTSTP, &amp;act1, NULL);
  
  
      while (1)
      {
          // Using exponential backoff for sleep time
  
          sleep(backoff);
          backoff = backoff &lt;&lt; 2;
  
          if (backoff &gt; MAX_BACKOFF)
              backoff = MAX_BACKOFF;
  
  
          // Check for signal flags
  
          if (flgsigint) 
          {
              printf("main: gonna do some SIGINT stuff\n");
              // stuff
              flgsigint = 0;
          }
  
          if (flgsigtstp)
          {
              printf("main: gonna do some SIGTSTP stuff\n");
              // stuff
              flgsigtstp = 0;
          }
  
      }
  
      exit(0);
  }
  
  
  </code></pre>
  
  <p>Now as far as I have tested it, it seems to be working ok. I'm mainly interested in catching the latest of a lets say, burst of <code>SIGINT</code>s happening at a specific time, so it doesn't matter if some are lost. </p>
  
  <p>Does my approach seem ok, or will I be surprised by weird behavior as I move on with my project?</p>
  
  <p>Also, is there something better to do about the waiting that main needs to do?</p>
  
  <p>Any help would be appreciated!</p>