How to block signals in C?

Jack picture Jack · Nov 20, 2012 · Viewed 9.4k times · Source

I'm trying to create a program that blocks the signal SIGUSR1 and the it unblocks the signal. In the middle I want to see that the signal is blocked using sigpending. But it always says that the signal isn't blocked, and I can use the signal when it's supposed to be blocked. This is the code that I have.

#include <stdlib.h>
#include <stdio.h>
#include <signal.h>

static void signals(int signaln) 
{
  switch (signaln) {
  case SIGUSR1:
    printf("Signal SIGUSR1\n"); break;
  }
  return; 
}
main()
{
  sigset_t set,set2;
  struct sigaction sigs;
  sigs.sa_handler = signals;
  sigemptyset(&sigs.sa_mask);
  sigs.sa_flags=SA_ONESHOT;
  sigaction(SIGUSR1, &sigs,0);
  sigemptyset(&set);
  sigemptyset(&set2);
  sigaddset(&set,SIGUSR1);
  if(sigprocmask(SIG_BLOCK, &set, NULL)==0){
    printf("Blocking SISGUSR1...\n");
  }
  sigpending(&set2);
  if (sigismember(&set2,SIGUSR1)==1)
  {
    printf("The signal is blocked\n");  //it should print this
  }
  wait(2);
  kill(getpid(),SIGUSR1); //the signal shouldn't work
  wait(2);
  if(sigprocmask(SIG_UNBLOCK, &set, NULL)==0){
    printf("Unblocking SIGUSR1\n");
  }
}

Could anyone help me?

Answer

rici picture rici · Nov 20, 2012

sigpending doesn't tell you whether a signal is blocked. It tells you whether a signal is waiting to be delivered. (i.e., the signal is blocked and one has been sent.)

Also, blocked doesn't meean that the signal won't be delivered; it means that the signal won't be delivered now. So you can send the signal, and it will be delivered as soon as the signal is unblocked; probably after the call to sigprocmask(SIGUNBLOCKED...) but before the call to printf, so you'll probably see the signal received message before you see the "unblocking" message.