Skip to content

Commit 5826885

Browse files
author
mgroot
committed
5.2: Signals | POSIX signals example done
1 parent 7795f10 commit 5826885

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)