开发者

UNIX / Linux signals

开发者 https://www.devze.com 2023-02-14 06:11 出处:网络
Can someone tell me, as to why program1\'s signal hand开发者_JAVA百科ler is not working? Program1: signal1.c

Can someone tell me, as to why program1's signal hand开发者_JAVA百科ler is not working?

Program1: signal1.c
 #include <stdio.h>
 #include <signal.h>

 void handler(int sig)
 {
  printf("Caught signal: %d",sig);
  signal(sig,handler);
 }

 int main()
 {
  struct sigaction sa;
  sa.sa_handler=handler;
  sa.sa_sigaction=NULL;
  sigaction(SIGRTMIN,&sa,NULL);
  kill(0,SIGRTMIN);
 }



Actual Output:
# ./a.out 
  Real-time signal 0

Expected Output:
  Caught signal: 34

Please help me to resolve Program1

However, the program2 works, if i use a simple signal handler like as usual:

enter code here
#include <stdio.h>
#include <signal.h>

void handler(int sig)
{
 printf("Caught signal: %d\n",sig);
 signal(sig,handler);
}

int main()
{
signal(SIGRTMIN, handler);
kill(0,SIGRTMIN);
}

Output:
Caught signal: 34


This is because sa_handler and sa_sigaction are members of the same union:

struct sigaction {
    union {
      __sighandler_t _sa_handler;
      void (*_sa_sigaction)(int, struct siginfo *, void *);
    } _u;
    sigset_t sa_mask;
    unsigned long sa_flags;
    void (*sa_restorer)(void);
};

#define sa_handler  _u._sa_handler
#define sa_sigaction    _u._sa_sigaction

So that when you set sa_sigaction it overwrites sa_handler value.

0

精彩评论

暂无评论...
验证码 换一张
取 消