-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
77 lines (63 loc) · 1.57 KB
/
main.cpp
File metadata and controls
77 lines (63 loc) · 1.57 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
70
71
72
73
74
75
76
77
#include <signal.h>
#include <assert.h>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
void handler(int signum)
{
if(signum == 1){
cout << "Signal SIGHUP arrived" << '\n';
}
else if(signum == 10){
cout << "Signal SIGUSR1 arrived" << '\n';
}
else if(signum == 29){
cout << "Signal SIGIO arrived" << '\n';
}
else{
cout << "I don't know this signal" << '\n';
}
}
int main(int arge, char** argv)
{
int f;
int status;
int pidp = getpid();
int pidc;
struct sigaction *act = new (struct sigaction);
act->sa_handler = handler;
sigemptyset (&(act->sa_mask));
act->sa_flags = 0;
assert (sigaction (SIGHUP, act, NULL) == 0);
act->sa_handler = handler;
sigemptyset (&(act->sa_mask));
act->sa_flags = 0;
assert (sigaction (SIGUSR1, act, NULL) == 0);
act->sa_handler = handler;
sigemptyset (&(act->sa_mask));
act->sa_flags = 0;
assert (sigaction (SIGIO, act, NULL) == 0);
if ((f = fork()) < 0){
perror("Error");
}
else if (f == 0){
pidc = getpid();
kill(pidc, SIGHUP);
kill(pidc, SIGUSR1);
kill(pidc, SIGIO);
kill(pidc, SIGIO);
kill(pidc, SIGIO);
}
else{
waitpid(f, &status, 0);
if(WIFEXITED(status))
cout << "Process " << f << " exited with status: " << WEXITSTATUS(status) << '\n';
else
cout << "Child didn't exit\n";
}
delete act;
return 0;
}