Newer
Older
#include <iostream>
#include <csignal>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <unistd.h>
#include <ctime>
#include <sys/wait.h>
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
volatile bool running = true;
void print_in_exit() {
std::cout << "Message during exit" << std::endl;
}
void handle_signal(int signal_number) {
std::cout << "Received signal: " << signal_number << std::endl;
std::cout << "Stopping the program" << std::endl;
running = false;
}
void print_process_info(const char* process_type) {
std::cout <<process_type << " | id: " << getpid() << std::endl;
std::cout <<process_type << " | father id: " << getppid() << std::endl;
std::cout <<process_type << " | groupd id: " << getpgrp() << std::endl;
std::cout << rand() % 100 << std::endl;
}
int main(){
std::cout << "started" << std::endl;
if (atexit(print_in_exit) != 0) {
perror("Fail in exit");
exit(EXIT_FAILURE);
}
struct sigaction sa;
sa.sa_handler = handle_signal;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGINT, &sa, nullptr) == -1) {
perror("Fail in exit");
exit(EXIT_FAILURE);
}
if (sigaction(SIGTERM, &sa, nullptr) == -1) {
perror("Fail in exit");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == 0) {
while (running) {
print_process_info("Child");
sleep(1);
}
} else {
while (running) {
print_process_info("Parent");
sleep(1);
int status;
pid_t result = waitpid(pid, &status, WNOHANG);
if (result == pid) {
std::cout << "child dead, father stopping" << std::endl;
running = false;
}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
}
}
std::cout << "finished" << std::endl;
return EXIT_SUCCESS;
}
/*
Ouput:
Parent | id: 26677
Parent | father id: 16628
Parent | groupd id: 26677
23
Child | id: 26678
Child | father id: 26677
Child | groupd id: 26677
23
Parent | id: 26677
Parent | father id: 16628
Parent | groupd id: 26677
67
Child | id: 26678
Child | father id: 26677
Child | groupd id: 26677
67
Parent | id: 26677
Parent | father id: 16628
Parent | groupd id: 26677
35
Child | id: 26678
Child | father id: 26677
Child | groupd id: 26677
35
Parent | id: 26677
Parent | father id: 16628
Parent | groupd id: 26677
29
Child | id: 26678
Child | father id: 26677
Child | groupd id: 26677
29
Parent | id: 26677
Parent | father id: 16628
Parent | groupd id: 26677
2
Child | id: 26678
Child | father id: 26677
Child | groupd id: 26677
2
^CReceived signal: 2
Received signal: 2
Stopping the program
Stopping the program
finished
finished
Message during exit
Message during exit
They have the same group id, and they both stop cin CTR+C because they belong to the same group,
and this singal is sent to all the process in the group
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
==============================================================================================================
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
2344 16628 16628 16628 pts/1 28184 Ss 911 0:02 /bin/zsh -i
2344 21290 21290 21290 pts/2 28302 Ss 911 0:00 /bin/zsh -i
16628 28184 28184 16628 pts/1 28184 S+ 911 0:00 ./a.out
28184 28185 28184 16628 pts/1 28184 S+ 911 0:00 ./a.out
21290 28302 28302 21290 pts/2 28302 R+ 911 0:00 ps aj
output:
58
Parent | id: 28184
Parent | father id: 16628
Parent | groupd id: 28184
58
Child | id: 28185
Child | father id: 28184
Child | groupd id: 28184
after killing the son:
36
Child | id: 28185
Parent | id: 28184
Child | father id: 28184
Child | groupd id: 28184
Parent | father id: 16628
5
Parent | groupd id: 28184
5
Child | id: 28185
Parent | id: 28184
Child | father id: 28184
Parent | father id: 16628
Child | groupd id: 28184
Parent | groupd id: 28184
46
46
Received signal: 15
Stopping the program
finished
Message during exit
Parent | id: 28184
Parent | father id: 16628
Parent | groupd id: 28184
29
AND
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
2344 16628 16628 16628 pts/1 28184 Ss 911 0:02 /bin/zsh -i
2344 21290 21290 21290 pts/2 28984 Ss 911 0:00 /bin/zsh -i
16628 28184 28184 16628 pts/1 28184 S+ 911 0:00 ./a.out
28184 28185 28184 16628 pts/1 28184 Z+ 911 0:00 [a.out] <defunct>
21290 28984 28984 21290 pts/2 28984 R+ 911 0:00 ps aj
the child continous there, but has a <defunct>
after killing first the parent:
PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND
2344 16628 16628 16628 pts/1 16628 Ss+ 911 0:02 /bin/zsh -i
2344 21290 21290 21290 pts/2 30012 Ss 911 0:01 /bin/zsh -i
1 29861 29860 16628 pts/1 16628 S 911 0:00 ./a.out
21290 30012 30012 21290 pts/2 30012 R+ 911 0:00 ps aj
the child continous to be running
If first I kill the child and then the father, both of them will desapear
After killing first the child:
Child | groupd id: 31529
29
Received signal: 15
Stopping the program
finished
Message during exit
child dead, father stopping
finished
Message during exit