-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsig2.c
43 lines (33 loc) · 959 Bytes
/
sig2.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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#define TRUE 1
void INThandler(int);
void main(void)
{
// A process can replace the default signal handler
// for almost all signals (but not SIGKILL) by its
// user’s own handler function.
// The SIGINT (“program interrupt”) signal is sent
// when the user types the INTR character (normally C-c)
signal(SIGINT, INThandler);
while (TRUE)
{
pause();
}
}
void INThandler(int sig)
{
char c;
// SIG_IGN: Ignores the signal sig. Passing SIG_IGN as
// handler ignores a given signal ignores it (except the
// signals SIGKILL and SIGSTOP which can't caught or ignored).
signal(sig, SIG_IGN);
printf("OUCH, did you hit Ctrl-C?\n Do you really want to quit? [y/n] ");
c = (char) getchar();
if (c == 'y' || c == 'Y')
exit (0);
else
signal(SIGINT, INThandler);
}