Skip to content

Commit af8f676

Browse files
committed
feat: 系统未决信号集、系统阻塞信号集、自定义信号集,以及信号捕捉处理
- 自定义信号集: - sigaddset() //将指定信号置为 1,添加到自定义集中 - sigdelset() //将指定信号置为 0,添加到自定义集中 - sigemptyset() //将所有信号置为 0,清空 - sigfillset() //将所有信号置为 1,填充 - sigismember() //判断指定信号是否存在,是否为 1 - 系统信号集: - sigprocmask() //将自定义信号集设置给阻塞信号集。 - sigprocmask() //读取当前信号的未决信号集。参数为输出参数,内核将未决信号集写入 set - 信号捕捉: - signal() //实现信号捕捉的功能;最简单使用一个函数。 - sigaction() //同上,多一个额外功能,运行期间能够**临时** 屏蔽指定信号
1 parent 8e2b18e commit af8f676

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed
8.41 KB
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/wait.h>
4+
#include <sys/time.h>
5+
#include <signal.h>
6+
7+
void func(int no); //用作回调函数
8+
9+
int main(int argc, char *argv[])
10+
{
11+
__sigaction_u sigactu;
12+
sigactu.__sa_handler = func; //另一个变量 sigactu.__sa_sigaction 不用赋值
13+
14+
struct sigaction act;
15+
act.sa_flags = 0; //通常给 0
16+
act.__sigaction_u = sigactu;
17+
sigemptyset(&act.sa_mask); //清空 自定义信号集
18+
sigaddset(&act.sa_mask, SIGQUIT); //向指定 系统阻塞集 中写入 自定义的信号集,添加需要屏蔽的信号(ctrl + 反斜杠 触发)
19+
20+
sigaction(SIGINT, &act, NULL);
21+
22+
while (true) {
23+
printf("Keep the thread running for the non-death state.\n");
24+
sleep(1);
25+
};
26+
27+
}
28+
29+
void func(int no)
30+
{
31+
printf("捕捉的信号为: %d\n", no);
32+
sleep(4);
33+
printf("醒了\n");
34+
}

unix_linux_15_sys_usr_signal/signal

8.41 KB
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/wait.h>
4+
#include <sys/time.h>
5+
#include <signal.h>
6+
7+
void func(int no); //用作回调函数,给 signal() 调用
8+
9+
int main(int argc, char *argv[])
10+
{
11+
signal(SIGINT, func); //设置信号捕捉函数, 捕捉 ctrl + c
12+
13+
while (true) {
14+
printf("Keep the thread running for the non-death state.\n");
15+
sleep(1);
16+
}
17+
18+
return 0;
19+
}
20+
21+
void func(int no)
22+
{
23+
printf("捕捉的信号为: %d\n", no);
24+
}
8.34 KB
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/wait.h>
4+
#include <sys/time.h>
5+
#include <signal.h>
6+
7+
int main(int argc, char *argv[])
8+
{
9+
while (true) {
10+
sigset_t pendest;
11+
sigpending(&pendest);
12+
13+
for (int i = 0; i < 64; i++) { //每隔一秒,对系统信号阻塞集的信号做一次校验
14+
if (sigismember(&pendest, i))
15+
printf("1");
16+
else
17+
printf("0");
18+
}
19+
20+
printf("\n");
21+
sleep(1);
22+
}
23+
24+
return 0;
25+
}

0 commit comments

Comments
 (0)