-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandling_signal_sigaction_ex.c
62 lines (47 loc) · 1.04 KB
/
handling_signal_sigaction_ex.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
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
int score = 0;
void end_game() {
printf("\nFinal score: %d\n", score);
exit(0);
}
int catch_signal(int sig, void (*handler)(int)) {
struct sigaction action;
action.sa_handler = handler;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
return sigaction (sig, &action, NULL);
}
void times_up() {
puts("\nTIME's UP!");
raise(SIGINT);
}
void error(char *msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(1);
}
int main() {
catch_signal(SIGALRM, times_up);
catch_signal(SIGINT, end_game);
srand(time(0));
while(1) {
int a = rand() % 11;
int b = rand() % 11;
char txt[4];
alarm(5);
printf("\nWhat is %d times %d: ", a, b);
fgets(txt, 4, stdin);
int answer = atoi(txt);
if(answer == a * b)
score++;
else
printf("\nWrong! Score: %d\n", score);
}
return 0;
}