-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry.c
58 lines (58 loc) · 1.13 KB
/
try.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <signal.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define START 0
#define FROM_CTRL_C 1
#define FROM_ALARM 2
#define ALARM 5
jmp_buf Buf;
void INT(int);
void ALRM(int);
void INT(int sig)
{
char c;
signal(SIGALRM, SIG_IGN);
signal(SIGINT, SIG_IGN);
printf("Want to quite?");
c = getchar();
if (c == 'y' || c == 'Y')
exit(0);
signal(SIGINT, INT);
signal(SIGALRM, ALRM);
longjmp(Buf, FROM_CTRL_C);
}
void ALRM(int sig)
{
signal(SIGINT, SIG_IGN);
signal(SIGALRM, SIG_IGN);
printf("got and alarm\n");
alarm(0); /* reset alarm */
signal(SIGALRM, ALRM);
signal(SIGINT, INT);
longjmp(Buf, FROM_ALARM);
}
int main(void)
{
int Return;
signal(SIGINT, INT);
signal(SIGALRM, ALRM);
while (1)
{
if ((Return = setjmp(Buf)) == START)
{
alarm(ALARM);
pause();
}
else if (Return == FROM_CTRL_C)
{
}
else if (Return == FROM_ALARM)
{
printf("Alarm reset to % d sec.\n",
ALARM);
alarm(ALARM);
}
}
}