File tree Expand file tree Collapse file tree 6 files changed +83
-0
lines changed
unix_linux_15_sys_usr_signal Expand file tree Collapse file tree 6 files changed +83
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments