- 论坛徽章:
- 0
|
这是APUE中一段代码(Program 10.15):
- #include <signal.h>;
- #include "ourhdr.h"
- static void sig_int(int);
- int
- main(void)
- {
- sigset_t newmask, oldmask, zeromask;
- if (signal(SIGINT, sig_int) == SIG_ERR)
- err_sys("signal(SIGINT) error");
- sigemptyset(&zeromask);
- sigemptyset(&newmask);
- sigaddset(&newmask, SIGINT);
- /* block SIGINT and save current signal mask */
- if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
- err_sys("SIG_BLOCK error");
- /* critical region of code */
- pr_mask("in critical region: ");
- /* allow all signals and pause */
- if (sigsuspend(&zeromask) != -1)
- err_sys("sigsuspend error");
- pr_mask("after return from sigsuspend: ");
- /* reset signal mask which unblocks SIGINT */
- if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
- err_sys("SIG_SETMASK error");
- /* and continue processing ... */
- exit(0);
- }
- static void
- sig_int(int signo)
- {
- pr_mask("\nin sig_int: ");
- return;
- }
复制代码
- 运行结果:
- $a.out
- in critical region: SIGINT
- ^C
- /*sigsuspend还没有返回,为什么执行sig_int时,signal masks中有 SIGINT? 第一次不知道是怎么想的,当时觉得是对的, 现在觉得执行sig_int时,signal masks 应还是zeromask的值 */
- in sig_int: SIGINT
- after return from sigsuspend: SIGINT
复制代码 |
|