How to block signals in C?

醉酒当歌 提交于 2019-12-23 03:22:38

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13481186/how-to-block-signals-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!