Signal Processing in C - Interrupt Interrupt

I was wondering if it is possible to interrupt a signal when my program processes another 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 every time 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

It's always like that?

+5
source share
2 answers

Quote sigaction(2)manpage:

Signal procedures are usually performed with the signal that caused their blocking to be blocked, but other signals may appear. The global signal mask defines a set of signals that are currently blocked from delivery to the process. The signal mask for the process is initialized with a signal mask (usually empty). It can be changed during a call sigprocmask(2)or when a signal is fed into the process.

, SA_NODEFER.

+6

, , , . , ( , SIGCLD, "" ) " ", . , X , raise ( SIGUSR1), .

, , , (MacOS):

If multiple signals are ready to be delivered at the same time, any signals that
could be caused by traps are delivered first.

( , SIGSEGV SIGBUS.) , : - - , , .

SA_NODEFER, , , .

SIGCLD System V, , SIG_DFL SIGCLD. (, SysV , SA_RESETHAND, .) - , SIG_IGN. , , , , . block/unblock SysV : SIGCLD signal(SIGCLD, handler);, . , - , wait -ed, SysV SIGCLD, . , , .

Linux . (, http://www.kernel.org/doc/man-pages/online/pages/man7/signal.7.html.

+5

All Articles