Signal handling in C - interrupt in interrupt
c, signals
Solution
Quoting the `sigaction(2)` manpage:
Signal routines normally execute with the signal that caused their invocation blocked, but other signals may yet occur. A global signal mask defines the set of signals currently blocked from delivery to a process. The signal mask for a process is initialized from that of its parent (normally empty). It may be changed with a `sigprocmask(2)` call, or when a signal is delivered to the process.
You can control whether the signal is automatically blocked in its signal handler with the `SA_NODEFER` flag.
Problem
I was wondering if it is possible to be interrupted by a signal when my program is handling other signal at the same time, I tried to simulate it with: ``` #include<signal.h> #include<stdlib.h> #include<stdio.h> #include<unistd.h> #include<sys/wait.h> #include<string.h> void sig_output() { sigset_t set; sigprocmask(0,NULL,&set); printf("currently blocking:"); if (sigismember(&set,SIGUSR1)) printf("\nSIGUSR1"); if(sigismember(&set,SIGUSR2)) printf("\nSIGUSR2"); printf("\n"); return ; } void sig_handler(int sig) { raise(SIGUSR1); printf("start\n"); if (sig==SIGUSR1) printf("SIGUSR1\n"); else if (sig==SIGUSR2) printf("SIGUSR2\n"); printf("end\n"); return ; } void other_sig_handler(int sig) { printf("start - other\n"); if (sig==SIGUSR1) printf("SIGUSR1\n"); else if (sig==SIGUSR2) printf("SIGUSR2\n"); printf("end - other\n"); return ; } int main() { sig_output(); struct sigaction a; a.sa_handler=sig_handler; a.sa_flags=0; sigset_t set,old; //blocking SIGUSR1,SIGUSR2 sigemptyset(&set); sigaddset(&set,SIGUSR1); sigaddset(&set,SIGUSR2); printf("blocking SIGUSR1, SIGUSR2\n"); sigprocmask(SIG_SETMASK,&set,&old); sig_output(); //adding handles for SIGUSR1,SIGUSR2 sigemptyset(&(a.sa_mask)); sigaction(SIGUSR1,&a,NULL); a.sa_handler=other_sig_handler; sigaction(SIGUSR2,&a,NULL); printf("poczatek wysylania \n"); raise(SIGUSR1); raise(SIGUSR2); raise(SIGUSR1); printf("using sigsuspend\n"); sigsuspend(&old); printf("end of program\n"); return 0; } ``` and everytime I run this program I get ``` currently blocking: blocking SIGUSR1, SIGUSR2 currently blocking: SIGUSR1 SIGUSR2 raising using sigsuspend start - other SIGUSR2 end - other start SIGUSR1 end end of program ``` is it always like that?