1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<signal.h>
5 #include<setjmp.h>
6 static jmp_buf env_alrm;
7
8
9 void alarm_signal(int a)
10 {
11 longjmp(env_alrm,1);
12
13 }
14
15 int main()
16 {
17
18 if(signal(SIGALRM,alarm_signal) == SIG_ERR)
19 {
20 printf("has error\n");
21 return 1;
22 }
23
24 if(setjmp(env_alrm)==0)
25 {
26 alarm(5);
27 pause();
28 }
29
30
31 printf("--------over---------\n");
32 return 0;
33
34
35
36
37 }
2 #include<stdlib.h>
3 #include<unistd.h>
4 #include<signal.h>
5 #include<setjmp.h>
6 static jmp_buf env_alrm;
7
8
9 void alarm_signal(int a)
10 {
11 longjmp(env_alrm,1);
12
13 }
14
15 int main()
16 {
17
18 if(signal(SIGALRM,alarm_signal) == SIG_ERR)
19 {
20 printf("has error\n");
21 return 1;
22 }
23
24 if(setjmp(env_alrm)==0)
25 {
26 alarm(5);
27 pause();
28 }
29
30
31 printf("--------over---------\n");
32 return 0;
33
34
35
36
37 }

本文介绍了一个简单的C程序示例,该程序利用POSIX信号处理机制中的SIGALRM信号来实现定时中断功能。通过注册一个信号处理函数并在指定时间后发送SIGALRM信号,程序能够在接收到信号后立即跳转到之前设置好的环境,从而达到定时中断的目的。
444

被折叠的 条评论
为什么被折叠?



