title: Signal control in C
body: <p>Is it possible to assure signal invoking order in C? Let's look at the following code:</p>
  
  <pre><code>//main.c
  int counter;
  pid_t pid;
  
  void alrm_handler(int s)
  {
      kill(pid, SIGUSR1);
      alarm(1);
      --counter;
      if (counter == 0)
          kill(pid, SIGTERM);
      printf("%d\n", counter);
      fflush(stdout);
  }
  
  int main(int argc, char* argv[])
  {
      counter = atoi(argv[1]);
      signal(SIGALRM, alrm_handler);
      alarm(1);
  
      pid = fork();
      if (!pid)
          execl("child", "child", NULL);
  
      wait(NULL);
  
      return 0;
  }
  
  //child.c
  void usr1_handler(int s)
  {
      puts("TEXT");
      fflush(stdout);
  }
  
  int main()
  {
      signal(SIGUSR1, usr1_handler);
  
      while(1)
      {
          struct timespec T;
          T.tv_nsec = 100;
          T.tv_sec = 0;
          nanosleep(&amp;T, NULL);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Even though I send SIG_USR1 3 times I get an output only 2 times. It seems that SIGTERM has higher priority or something. How is it possible to achieve 3 outputs in this scenario?</p>