-
Notifications
You must be signed in to change notification settings - Fork 4
/
7_1_signal_handler.c
57 lines (46 loc) · 945 Bytes
/
7_1_signal_handler.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
/*
* @Author: Nam
* @Date: 2018-05-15 01:14:33
* @Last Modified by: Nam
* @Last Modified time: 2018-05-15 01:16:22
*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#define SIG_OK 0
#define SIG_NG -1
#define LOOP_ON 0
#define LOOP_OFF 1
int loopFlag = LOOP_ON;
// fuction to handle each signal
void sigHandleSIGALRM ();
void sigHandleSIGINT ();
int main(int argc, char const *argv[]) {
if (argc != 2) {
printf("Argument should be 1 number\n");
return -1;
}
signal(SIGALRM, sigHandleSIGALRM);
signal(SIGINT, sigHandleSIGINT);
printf("Cat is attacking! Ctrl - C quick!!!\n");
// call alarm signal SIGALRM
alarm(atoi(argv[1]));
// infinity loop
while (loopFlag == LOOP_ON) {
sleep(1);
printf(".\n");
}
}
void sigHandleSIGALRM ()
{
printf("CAT ATTACK!!!\n");
usleep(500000);
printf("You died.\n");
exit(SIG_OK);
}
void sigHandleSIGINT ()
{
printf("at died.\n");
exit(SIG_OK);
}