Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real-time signals received in reversed order on Linux

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.

$ cat realtime.c
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

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->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) < 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(&action.sa_mask);
    action.sa_flags = SA_SIGINFO;
    if ((sigaction(SIGRTMIN + 1, & action, NULL) < 0)
     || (sigaction(SIGRTMIN + 2, & action, NULL) < 0)
     || (sigaction(SIGRTMIN + 3, & action, NULL) < 0)) {
        perror("sigaction");
        exit(EXIT_FAILURE);
    }

    // Block all signals
    sigfillset(&set);
    sigprocmask(SIG_BLOCK, &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(&set);
    sigprocmask(SIG_UNBLOCK, &set, NULL);

    // To make sure we're handling all signals before resuming
    sleep(1);

    // Display results
    for (i = 0; i < received_signals_count; ++i) {
        printf("Received signal SIGRTMIN+%d, value %d\n",
               received_signals[i], received_signals_value[i]);
    }

    return EXIT_SUCCESS;
}

I consistently get the output below:

$ 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

Which contradicts what's written in signal(7) (http://man7.org/linux/man-pages/man7/signal.7.html):

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.

As far as I understand, I should get:

$ ./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

I've had a quick look at the Linux kernel source code but couldn't find (yet) where real-time signals where handled.

Am I doing something wrong / missing something or is the man page inaccurate?

like image 522
Timothée Ravier Avatar asked Oct 18 '25 13:10

Timothée Ravier


2 Answers

Your problem is that you are permitting other signals to interrupt your running signal handler:

sigemptyset(&action.sa_mask);

The sa_mask field specifies the set of signals that aren't allowed to interrupt the execution of this handler.

This means that you get SIGRTMIN + 1 and before you can do anything about it you get another, lower-priority signal which you then start to handle. That signal in turn then gets interrupted in by yet another signal, and so on.

To fix this, disallow any other signals from interrupting your handler:

sigfillset(&action.sa_mask);
like image 174
cnicutar Avatar answered Oct 21 '25 02:10

cnicutar


I think that your problem is this line sigemptyset(&action.sa_mask);.

The signals are delivered in the correct order RT1 -> RT3, but the higher priority signals interrupt the lower priority ones as they are sent later.

Within each priority level the signals are delivered in the order sent, and as by default the signal being delivered is blocked during delivery they are correctly handled in the order sent, this results in your mixed up ordering.

As cnicutar says, switch to using sigfillset and all will be good.

like image 32
Andrew Avatar answered Oct 21 '25 03:10

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!