There are several timers that generate the same SIGRTMIN signal, additionally passing in a sival_int value. 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. calling init_timer, I simply change the handler for a particular signal, because can he be only one?

  • 3
    As far as I understand * nix, only one handler can be active for each type of signal (and how were you going to make several handlers for the same signal?). But different timers can generate different signals, then for each timer you can have your own handler. All real-time signals will reach guaranteed. Ordinary signals of the same type do not accumulate. - avp

1 answer 1

I do not 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, it is processed there and DISAPPEARS! One signal cannot reach two handlers. The signal guide says that if several processors are waiting for a signal, then who exactly gets it is determined by the implementation.

  • You write: В руководстве по сигналам написано, что если сигнал ожидают несколько обработчиков... If you mean a multithread program, please specify. Better yet, give a specific link to manpage. - avp
  • one
    The published message does not contain a solution. - I do not understand what solution? The question was: is the signal guaranteed to reach all signal handlers? and the answer to it can only be yes / no. I answered - "no." Something remains unclear? - Sergey
  • one
    If you mean the multithread program - not necessarily. Signals are transmitted between processes. A special case of which is thread. But I can send a signal from one SINGLE-Stream program to another SINGLE-Stream program. - Sergey
  • one
    Better a specific link to the manpage is impossible, because The life cycle of a signal is not described in any particular man-e, but in the IEEE Std 1003.1b-1993 standard. The intuit.ru website has a very good course on POSIX-RT: intuit.ru/studies/courses/53/53/info But, in fact, even this course is not necessary to study. The idea is very simple: after the signal is intercepted and processed, it is deleted by the OS and no longer goes anywhere. - Sergey