-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpending.cpp
More file actions
69 lines (38 loc) · 1.78 KB
/
pending.cpp
File metadata and controls
69 lines (38 loc) · 1.78 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Angshuman Ghosh 2017CS01
Program to block some specific signals and show the blocked or pending signals
*/
#include <iostream>
#include <set>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
using namespace std;
int main(){
int o_size = 0;
set<int> signals; // set of blocked signals
set<int>::iterator it;
sigset_t block, wait; // signal set
sigemptyset(&block);
// add the signals to be blocked
sigaddset(&block, SIGINT);
sigaddset(&block, SIGTERM);
sigaddset(&block, SIGQUIT);
sigprocmask(SIG_BLOCK, &block, NULL); // block the signals specified in signal set
while(1){
sigpending(&wait); // fill the signal set wait with pending signals
// check for the signals that are blocked
if(sigismember(&wait, SIGINT))
signals.insert(SIGINT);
if(sigismember(&wait, SIGTERM))
signals.insert(SIGTERM);
if(sigismember(&wait, SIGQUIT))
signals.insert(SIGQUIT);
if(o_size < signals.size()){ // print the signals when there is a new blocked signal
for(it = signals.begin() ; it!=signals.end() ; it++){
cout << ((*it == SIGINT) ? "SIGINT" : (*it == SIGTERM) ? "SIGTERM" : "SIGQUIT" ) << endl;
}
cout << "====================" << endl;
}
o_size = signals.size(); // update the signal size keeper var
}
}