-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal_handler.c
49 lines (45 loc) · 1.4 KB
/
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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
//////////////////////////////Signal handler for Alarm/////////////////////////////////////
void alarm_handler(int siga){
if (siga == SIGALRM){
printf ("Alarm\n");
}
}
//////////////////////////////Signal handler for Interrupt/////////////////////////////////
void interrupt_handler(int sigb){
if (sigb == SIGINT){
printf ("CTRL+C pressed!\n");
}
}
//////////////////////////////Signal handler for Terminal Stop/////////////////////////////
void termstop_handler(int sigc){
if (sigc == SIGTSTP){
printf ("CTRL+Z pressed!\n");
exit(1);
}
}
/////////////////////////////////////////// MAIN //////////////////////////////////////////
int main(void){
// Register the signal handler for SIGALRM, SIGINT and SIGTSTP AND
// Checks for Errors in the registry of the handlers to the significant signals////////////
if (signal(SIGALRM , alarm_handler) == SIG_ERR){
printf (" Failed to register Alarm handler. ");
exit (1);
}
if (signal(SIGINT , interrupt_handler) == SIG_ERR){
printf (" Failed to register interrupt handler. ");
exit (1);
}
if (signal(SIGTSTP , termstop_handler) == SIG_ERR){
printf (" Failed to register Terminal Stop handler. ");
exit (1);
}
///////////////////////////////////////////////////////////////////////////////////////////
while (1){
alarm (2); // set alarm to fire in 2 seconds.
sleep(2);
}
}