1 Star 0 Fork 0

godeyer / clang

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
sigset.c 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
godeyer 提交于 2024-01-19 14:10 . 初次提交
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
// 信号的处理动作
void callback(int num)
{
printf("当前捕捉的信号: %d\n", num);
}
int main()
{
// 1. 初始化信号集
sigset_t myset;
sigemptyset(&myset);
// 设置阻塞的信号
sigaddset(&myset, SIGINT); // 2
sigaddset(&myset, SIGQUIT); // 3
sigaddset(&myset, SIGKILL); // 9 测试不能被阻塞
// 当阻塞的信号被解除阻塞, 该信号就可以被捕捉到了
// 如果信号被捕捉到之后, 马上就被处理掉了 --> 递达状态
struct sigaction act;
act.sa_handler = callback;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(SIGINT, &act, NULL);
// 和sigint的处理动作相同
sigaction(SIGQUIT, &act, NULL);
sigaction(SIGKILL, &act, NULL);
// 2. 将初始化的信号集中的数据设置给内核
sigset_t old;
sigfillset(&myset);
sigprocmask(SIG_BLOCK, &myset, &old);
// 3. 让进程一直运行, 在当前进程中产生对应的信号
int i = 0;
while(1)
{
// 4. 读内核的未决信号集
sigset_t curset;
sigpending(&curset);
// 遍历这个信号集
for(int i=1; i<32; ++i)
{
int ret = sigismember(&curset, i);
printf("%d", ret);
}
printf("\n");
sleep(1);
i++;
if(i==10)
{
printf(" 已经运行了%d 次 ==================\n",i);
// 解除阻塞, 重新设置阻塞信号集
//sigprocmask(SIG_UNBLOCK, &myset, NULL);
sigprocmask(SIG_SETMASK, &old, NULL);
}
}
return 0;
}
//程序中对 9 号信号的捕捉是无效的,因为它无法被捕捉。
1
https://gitee.com/zzzman/clang.git
git@gitee.com:zzzman/clang.git
zzzman
clang
clang
master

搜索帮助