我正在尝试从网上找到的示例程序中理解Linux信号的工作原理,但其中有些部分我并不是很理解。
这是我的示例程序:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
void catcher(int sig) {
printf("catcher() has gained control\n");
}
int main(int argc, char *argv[]) {
struct sigaction sigact;
sigset_t sigset;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigact.sa_handler = catcher;
sigaction(SIGUSR1, &sigact, NULL);
printf("before first kill()\n");
kill(getpid(), SIGUSR1);
sigemptyset(&sigset);
sigaddset(&sigset, SIGUSR1);
sigprocmask(SIG_SETMASK, &sigset, NULL);
printf("before second kill()\n");
kill(getpid(), SIGUSR1);
printf("after second kill()\n");
return 0;
}
这是程序输出的样本:
在第一个kill()之前
catcher() 已获取控制权
在第二个kill()之前
在第二个kill()之后
我能知道为什么输出中第一行是before first kill()吗?为什么不是catcher() has gained control先出现?
据我所知,sa_handler由两种类型的信号组成,即默认信号和忽略信号。
我们如何知道它将生成哪个信号?如果生成忽略信号,为什么会触发函数打印catcher() has gained control?
此外,这个程序中的sa_mask函数是什么?据我理解,sa_mask会阻塞指定的信号。