-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsig_demo.c
59 lines (49 loc) · 1.24 KB
/
sig_demo.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
//Keith McKinley
//CS311-400
//Homework #4
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<signal.h>
#include<sys/types.h>
static void sigIntHandler(int sig){
printf("SIGINT has been caught, terminating the program\n");
kill(getpid(), -9);
}
static void sigUsr1Handler(int sig){
printf("SIGUSR1 has been caught\n");
return;
}
static void sigUsr2Handler(int sig){
printf("SIGUSR2 has been caught\n");
return;
}
int main(int argc, char **argv) {
struct sigaction sigInt, sigUsr1, sigUsr2, oldInt, oldUsr1, oldUsr2;
//setup
sigInt.sa_handler = sigIntHandler;
sigInt.sa_flags = 0;
sigUsr1.sa_handler = sigUsr1Handler;
sigUsr1.sa_flags = 0;
sigUsr2.sa_handler = sigUsr2Handler;
sigUsr2.sa_flags = 0;
//define handlers
sigaction (SIGINT, NULL, &oldInt);
if (oldInt.sa_handler != SIG_IGN)
sigaction (SIGINT, &sigInt, NULL);
sigaction (SIGUSR1, NULL, &oldUsr1);
if (oldUsr1.sa_handler != SIG_IGN)
sigaction (SIGUSR1, &sigUsr1, NULL);
sigaction (SIGINT, NULL, &oldUsr2);
if (oldUsr2.sa_handler != SIG_IGN)
sigaction (SIGUSR2, &sigUsr2, NULL);
//do it!
kill(getpid(), SIGUSR1);
kill(getpid(), SIGUSR2);
kill(getpid(), SIGINT);
return 0;
}
//end