- 论坛徽章:
- 0
|
在apue1中的代码是这样:
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
in sig_int: SIGINT
after return from sigsuspend: SIGINT
对在in critical region后所出现SIGINT 的解释是这样的:
sigsuspend的整个原子操作过程为:
(1) 设置新的mask阻塞当前进程;
(2) 收到信号,恢复原先mask;
(3) 调用该进程设置的信号处理函数;
(4) 待信号处理函数返回后,sigsuspend返回。
而当我看apue2的代码的时候,感觉如果按照上面的原子操作就有点问题,请高手指点下:
下面是apue2的代码
#include "apue.h"
static void sig_int(int);
int
main(void)
{
sigset_t newmask, oldmask, waitmask;
pr_mask("program start: ");
if (signal(SIGINT, sig_int) == SIG_ERR)
err_sys("signal(SIGINT) error");
sigemptyset(&waitmask);
sigaddset(&waitmask, SIGUSR1);
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: ");
/*
* Pause, allowing all signals except SIGUSR1.
*/
if (sigsuspend(&waitmask) != -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 ...
*/
pr_mask("program exit: ");
exit(0);
}
static void
sig_int(int signo)
{
pr_mask("\nin sig_int: ");
}
结果:
$ ./a.out
program start:
in critical region: SIGINT
^? type the interrupt character
in sig_int: SIGINT SIGUSR1
after return from sigsuspend: SIGINT
program exit:
若按照上面那个sigsuspend的理解:
(1) 设置新的mask阻塞当前进程;
那么此时的mask为waitmask
(2) 收到信号,恢复原先mask;
收到信号SIGINT, 先恢复mask--->此时的mask就为 newmask与oldmask并集的那个mask鸟 ( sigprocmask(SIG_BLOCK, &newmask, &oldmask) )
而在此mask中SIGUSR1并没阻塞
(3) 调用该进程设置的信号处理函数;
调用sig_int函数--->此时的mask已经还原,所以只有SIGINT,不会出现SIGUSR1. 但是程序的结果是in sig_int: SIGINT SIGUSR1. 我想不通为什么会出现SIGUSR1
(4) 待信号处理函数返回后,sigsuspend返回。 |
|