title: Real-time signals received in reversed order on Linux
body: <p>This program sends real-time signals to itself and handles them. Once they are handled, it outputs the signals it received in the order it received them.</p>
  
  <pre><code>$ cat realtime.c
  #include &lt;signal.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  
  int received_signals[10];
  int received_signals_value[10];
  int received_signals_count = 0;
  
  void real_time_handler(int sig_number, siginfo_t * info,
                         void * arg __attribute__ ((unused)))
  {
      received_signals[received_signals_count] = sig_number - SIGRTMIN;
      received_signals_value[received_signals_count] = info-&gt;si_value.sival_int;
      ++received_signals_count;
  }
  
  void send_real_time_signal(int sig_number, int value)
  {
      union sigval sig_value;
  
      printf("Sending signal SIRTMIN+%d, value %d\n", sig_number, value);
      sig_value.sival_int = value;
      if (sigqueue(getpid(), sig_number + SIGRTMIN, sig_value) &lt; 0) {
          perror("sigqueue");
          exit(EXIT_FAILURE);
      }
  }
  
  int main()
  {
      struct sigaction action;
      sigset_t set;
      int i;
  
      // Handler setup
      action.sa_sigaction = real_time_handler;
      sigemptyset(&amp;action.sa_mask);
      action.sa_flags = SA_SIGINFO;
      if ((sigaction(SIGRTMIN + 1, &amp; action, NULL) &lt; 0)
       || (sigaction(SIGRTMIN + 2, &amp; action, NULL) &lt; 0)
       || (sigaction(SIGRTMIN + 3, &amp; action, NULL) &lt; 0)) {
          perror("sigaction");
          exit(EXIT_FAILURE);
      }
  
      // Block all signals
      sigfillset(&amp;set);
      sigprocmask(SIG_BLOCK, &amp;set, NULL);
  
      send_real_time_signal(1, 0);
      send_real_time_signal(2, 1);
      send_real_time_signal(3, 2);
      send_real_time_signal(1, 3);
      send_real_time_signal(2, 4);
      send_real_time_signal(3, 5);
      send_real_time_signal(3, 6);
      send_real_time_signal(2, 7);
      send_real_time_signal(1, 8);
      send_real_time_signal(3, 9);
  
      // Unblock all signals
      sigfillset(&amp;set);
      sigprocmask(SIG_UNBLOCK, &amp;set, NULL);
  
      // To make sure we're handling all signals before resuming
      sleep(1);
  
      // Display results
      for (i = 0; i &lt; received_signals_count; ++i) {
          printf("Received signal SIGRTMIN+%d, value %d\n",
                 received_signals[i], received_signals_value[i]);
      }
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>I consistently get the output below:</p>
  
  <pre><code>$ gcc -Wall -Wextra -O2 -o realtime realtime.c
  $ ./realtime
  Sending signal SIRTMIN+1, value 0
  Sending signal SIRTMIN+2, value 1
  Sending signal SIRTMIN+3, value 2
  Sending signal SIRTMIN+1, value 3
  Sending signal SIRTMIN+2, value 4
  Sending signal SIRTMIN+3, value 5
  Sending signal SIRTMIN+3, value 6
  Sending signal SIRTMIN+2, value 7
  Sending signal SIRTMIN+1, value 8
  Sending signal SIRTMIN+3, value 9
  Received signal SIGRTMIN+3, value 2
  Received signal SIGRTMIN+3, value 5
  Received signal SIGRTMIN+3, value 6
  Received signal SIGRTMIN+3, value 9
  Received signal SIGRTMIN+2, value 1
  Received signal SIGRTMIN+2, value 4
  Received signal SIGRTMIN+2, value 7
  Received signal SIGRTMIN+1, value 0
  Received signal SIGRTMIN+1, value 3
  Received signal SIGRTMIN+1, value 8
  </code></pre>
  
  <p>Which contradicts what's written in signal(7) (<a href="http://man7.org/linux/man-pages/man7/signal.7.html" rel="nofollow">http://man7.org/linux/man-pages/man7/signal.7.html</a>):</p>
  
  <blockquote>
    <p>Real-time signals are delivered in a guaranteed order. Multiple real-time signals of the same type are delivered in the order they were sent. If different real-time signals are sent to a process, they are delivered starting with the lowest-numbered signal. (I.e., low-numbered signals have highest priority.) By contrast, if multiple standard signals are pending for a process, the order in which they are delivered is unspecified.</p>
  </blockquote>
  
  <p>As far as I understand, I should get:</p>
  
  <pre><code>$ ./realtime
  Sending signal SIRTMIN+1, value 0
  Sending signal SIRTMIN+2, value 1
  Sending signal SIRTMIN+3, value 2
  Sending signal SIRTMIN+1, value 3
  Sending signal SIRTMIN+2, value 4
  Sending signal SIRTMIN+3, value 5
  Sending signal SIRTMIN+3, value 6
  Sending signal SIRTMIN+2, value 7
  Sending signal SIRTMIN+1, value 8
  Sending signal SIRTMIN+3, value 9
  Received signal SIGRTMIN+1, value 0
  Received signal SIGRTMIN+1, value 3
  Received signal SIGRTMIN+1, value 8
  Received signal SIGRTMIN+2, value 1
  Received signal SIGRTMIN+2, value 4
  Received signal SIGRTMIN+2, value 7
  Received signal SIGRTMIN+3, value 2
  Received signal SIGRTMIN+3, value 5
  Received signal SIGRTMIN+3, value 6
  Received signal SIGRTMIN+3, value 9
  </code></pre>
  
  <p>I've had a quick look at the Linux kernel source code but couldn't find (yet) where real-time signals where handled.</p>
  
  <p>Am I doing something wrong / missing something or is the man page inaccurate?</p>