-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignaldemo.cpp
More file actions
53 lines (41 loc) · 893 Bytes
/
signaldemo.cpp
File metadata and controls
53 lines (41 loc) · 893 Bytes
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
/** This program illustrates a simple signal handler
* for the case where the user presses Ctrl-C. Test
* it by running it and pressing Ctrl-C.
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* The counter which counts the number
* of times the user pressed Ctrl-C
*/
int count = 9;
/**
* This function handles the singnal
* @param arg - the signal number
*/
void signalHandlerFunc(int arg)
{
/* We can take more Ctrl-Cs */
if(count > 0)
fprintf(stderr, "Haha I have %d lives!\n", count);
else
{
fprintf(stderr, "Ahh you got me!\n");
exit(0);
}
--count;
}
int main()
{
/* Overide the default signal handler for the
* SIGINT signal with signalHandlerFunc
*/
signal(SIGINT, signalHandlerFunc);
/* Spin the loop forever */
for(;;)
{
sleep(1);
}
return 0;
}