wangzhengquan
2020-10-21 82bd40bc2b2d024ab7be0f27eea3f871f99cc213
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "usg_common.h"
static void sig_quit(int);
int
main(void)
{
  sigset_t
  newmask, oldmask, pendmask;
  if (signal(SIGQUIT, sig_quit) == SIG_ERR)
    err_exit(errno, "can’t catch SIGQUIT");
  /*
  * Block SIGQUIT and save current signal mask.
  */
  sigemptyset(&newmask);
  sigaddset(&newmask, SIGQUIT);
  if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
    err_exit(errno, "SIG_BLOCK error");
  sleep(5);
  /* SIGQUIT here will remain pending */
  if (sigpending(&pendmask) < 0)
    err_exit(errno, "sigpending error");
  if (sigismember(&pendmask, SIGQUIT))
    printf("\nSIGQUIT pending\n");
  /*
  * Restore signal mask which unblocks SIGQUIT.
  */
  if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
    err_exit(errno, "SIG_SETMASK error");
  printf("SIGQUIT unblocked\n");
  sleep(5);
  exit(0);
  /* SIGQUIT here will terminate with core file */
}
 
static void sig_quit(int signo)
{
  printf("caught SIGQUIT\n");
  if (signal(SIGQUIT, SIG_DFL) == SIG_ERR)
    err_exit(errno, "can’t reset SIGQUIT");
}