1

我正在尝试创建一个程序来阻止信号 SIGUSR1 并解除对信号的阻止。在中间我想看到信号被阻止了sigpending。但是它总是说信号没有被阻塞,我可以在信号被阻塞的时候使用它。这是我拥有的代码。

#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");
  }
}

有人可以帮我吗?

4

1 回答 1

3

sigpending不会告诉你信号是否被阻塞。它告诉您信号是否正在等待传递。(即信号被阻塞已发送一个。)

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.

于 2012-11-20T20:17:43.467 回答