Question

I have set a handler for each signal (SIGCHLD, SIGTSTP, SIGINT), now I need to block other signals while some handler running .
There is some resources like sigaction(2) and Blocking for Handler , but I didn't understand what should I do in my situation or how to use the code .
little information :
handlers : sigchldHandler, sigintHandler, sigtstpHander => signals.c
there is a file called smash.c which contains an infinite loop to get commands all the time .
thanks in advance

Was it helpful?

Solution

When setting up the sigaction, you can specify a mask of signals that should be blocked when the handler is running.

You use it like so:

struct sigaction act;
sigset_t set;

memset(&act,0,sizeof act);
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGSTP);

act.sa_mask = set;
act.sa_handler = sigchldHandler;
act.sa_flags = SA_RESTART;

sigaction(SIGCHLD, &act, NULL);

This will block SIGUSR1 and SIGSTP while your handler for SIGCHLD is running. Do the same for your 2 other handlers as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top