sigtimedwait()的“set”参数中的信号未送达

时间:2016-04-17 09:52:11

标签: linux signals signal-handling sigaction sigprocmask

我最近一直致力于Linux上的信号处理,并已阅读了与信号处理相关的所有概念。一个问题是修补我的问题,为什么sigtimedwait()集合中的信号在解锁过程中不会被传递。我的代码如下: -

#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
void sighandler1(int sig)
{
        printf("SIGINT caught\n");
}
void sighandler2(int sig)
{
        printf("SIGTSTP caught\n");
}
void sighandler3(int sig)
{
        printf("SIGALRM caught\n");
}

int main()
{

  sigset_t s1,s2;

  struct sigaction act1,act2,act3;

  int ret;

  sigemptyset(&s1);// The bit-mask s1 is cleared
  sigaddset(&s1,SIGINT);//Add SIGINT to the bit-mask s1

  sigemptyset(&s2);// The bit-mask s2 is cleared
  sigaddset(&s2,SIGALRM);//Add SIGALRM to the bit-mask s2

  sigprocmask(SIG_BLOCK,&s2,NULL);//Signal(s) in s2 blocked

  act1.sa_handler = sighandler1; //function pointer pointing to the signal handler
  act1.sa_flags = 0;        

  sigaction(SIGINT,&act1,NULL);  // installing the action
                                 // for SIGINT

  act2.sa_handler = sighandler2; //function pointer pointing to another signal handler
  act2.sa_flags = 0;         // no flags


  sigaction(SIGTSTP,&act2,NULL);  // installing the action
                                 // for SIGTSTP
  act3.sa_handler = sighandler3; //function pointer pointing to another signal handler
  act3.sa_flags = 0;         // no flags


  sigaction(SIGALRM,&act3,NULL);  // installing the action for SIGALRM

  sigprocmask(SIG_SETMASK,&s1,NULL); //Signals in s1 blocked and other signals unblocked
  printf("sigprocmask() called with SIG_SETMASK on s1,which contains SIGINT\n");
  printf("Blocked on sigtimedwait() with s1\n");
  if(sigtimedwait(&s1,NULL,NULL) < 0)
        {
                if(errno ==  EINTR)
                printf("Some other signal caught\n");
        }
  printf("This is a process. You can pass signal to it\n");

  while(1);

}

为了更清楚这个问题,我在上面的代码中调用了sigtimedwait,并将“s1”作为“set”参数。该集仅包含信号SIGINT。根据手册页,sigtimedwait()会阻止进程,直到其集合中的一个信号被传递。我对这句话一切都很好。但是,当我传递SIGINT以解除阻塞进程时,为什么不调用SIGINT处理程序?另一方面,当我传递SIGALRM或SIGTSTP时,它们不在集合中,EINTR按预期返回,并且信号处理程序也被调用。

对于想要观察场景的任何人,可以执行上述代码,然后将SIGINT传递给它。他们将观察到在没有调用处理程序的情况下解锁该进程。为什么不调用处理程序?我是否误解了 sigtimedwait()的手册页的任何部分 ??

1 个答案:

答案 0 :(得分:0)

sigtimedwait似乎返回信号值,而不是信号处理程序被捕获:

switch(sigtimedwait(&s1,NULL,NULL))
{
    default:
        printf ("Some other signal???");
        break;
    case SIGINT:
        printf ("We got SIGINT\n");
        break;
    case -1:
        perror ("sigtimedwait");
        break;
}