File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
5_Processes_Signals_Files_Pipes/2_Signals Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < signal.h>
3
+ #include < unistd.h>
4
+
5
+ int * global_to_free;
6
+
7
+
8
+ void cleanup_handler (int status)
9
+ {
10
+ std::cout << " \n {HANDLER WAS CALLED}\n " << std::endl;
11
+ delete[] global_to_free;
12
+ }
13
+
14
+
15
+ void configure_signal ()
16
+ {
17
+ // set up sigset (block all)
18
+ sigset_t sigset;
19
+ sigemptyset (&sigset);
20
+
21
+ // set
22
+ struct sigaction act;
23
+ act.sa_mask = sigset;
24
+ act.sa_flags = SA_RESETHAND | SA_RESTART; // avoid double free + continue pause
25
+ act.__sigaction_u .__sa_handler = cleanup_handler;
26
+
27
+ sigaction (SIGINT, &act, NULL );
28
+ }
29
+
30
+
31
+ int main ()
32
+ {
33
+ global_to_free = new int [1024 ];
34
+
35
+ configure_signal ();
36
+
37
+ std::cout << " Waiting for ^C..." << std::endl;
38
+ pause ();
39
+
40
+ std::cout << " Exiting programm..." << std::endl;
41
+ return 0 ;
42
+ }
You can’t perform that action at this time.
0 commit comments