1

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

Rawhi
  • 6,155
  • 8
  • 36
  • 57

1 Answers1

6

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.

djanderson
  • 563
  • 4
  • 13
nos
  • 223,662
  • 58
  • 417
  • 506
  • Should I put this code before the infinite loop or inside each handler ? – Rawhi Nov 16 '13 at 00:23
  • @Rawhi This code is used to set up the signal handlers so the signal handlers can be called. If you place it inside your signal handlers, when will this code be run ? It will never be run. If you place it after your infinte loop, it will never be run. If you place it inside the infinite loop, it will run on every iteration, which is just a waste. Place it before the infinite loop. – nos Nov 16 '13 at 00:29