Linux Timers and Real Time Signals

Question:

There are several timers that generate the same SIGRTMIN signal by additionally passing a value to sival_int. There are several timer signal handlers. Do I understand correctly that the signal is guaranteed to reach all signal handlers? And if the signal is generated simultaneously by several timers?

Is it correct to check sival_int in the handler?

#define TIMER_EVENT_ID_1 1
#define TIMER_EVENT_ID_2 2

void timer_handler(int signo, siginfo_t *info, void *context)
{
    if(info->si_value.sival_int==TIMER_EVENT_ID_1)
    {
        sem_post(&sem1); //будим трид 
    }   

    if(info->si_value.sival_int==TIMER_EVENT_ID_2)
    {
        sem_post(&sem2); //будим трид 
    }
}

In different threads, I create, and then reset and set the values ​​of the timers:

typedef struct 
{
    struct sigevent sigev;
    struct sigaction sa;
    struct itimerspec ival;
    timer_t tid;
} timer_data_t;

int init_timer(timer_data_t *timer_data, void *handler, UNS8 sigNum, UNS8 eventNum)
{
    timer_data->sa.sa_flags = SA_SIGINFO;
    sigemptyset(&timer_data->sa.sa_mask);
    sigaddset(&timer_data->sa.sa_mask, sigNum);
    timer_data->sa.sa_sigaction=handler;   

    if(sigaction(sigNum, &timer_data->sa, NULL)==-1)
    {
        perror("sigaction failed");
        return -1;
    }

    timer_data->sigev.sigev_notify = SIGEV_SIGNAL;
    timer_data->sigev.sigev_signo = sigNum;
    timer_data->sigev.sigev_value.sival_int=eventNum;

    //создаем таймер
    if( timer_create(CLOCK_REALTIME, &timer_data->sigev, &timer_data->tid)==-1)
    {
        printf("init_timer: не могу создать таймер! \n");
        perror("timer_create");
        return -1;
    }

    return 0;       
}

int set_timer(timer_data_t *timer_data, UNS32 timeSec, UNS64 timeNsec)
{
    timer_data->ival.it_value.tv_sec=timeSec;
    timer_data->ival.it_value.tv_nsec =timeNsec;
    timer_data->ival.it_interval.tv_sec=0;
    timer_data->ival.it_interval.tv_nsec=0;

    if(timer_settime(timer_data->tid, 0, &timer_data->ival, NULL) == -1)
    {
        perror("timer_settime");
        return -1;
    }

    return 0;
}

Those. by calling init_timer I just change the handler for a specific signal, because can there be only one?

Answer:

I don't really understand your question:

Do I understand correctly that the signal is guaranteed to reach all signal handlers?"

What does "to all" mean? The signal always reaches the signal handler, is processed there and DISAPPEARS! One signal cannot reach two handlers. The signal manual says that if multiple handlers are waiting for a signal, it is up to the implementation to decide who exactly will receive it.

Scroll to Top