Newer
Older
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
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
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <csignal>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <sys/wait.h>
volatile bool running = true;
int pipefd[2]; // File descriptors for pipe
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;
}
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);
}
// Create the pipe
if (pipe(pipefd) == -1) {
perror("Pipe failed");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == 0) {
close(pipefd[1]);
int number;
while (running) {
ssize_t bytes_read = read(pipefd[0], &number, sizeof(number));
if (bytes_read > 0) {
std::cout << "Child received: " << number << std::endl;
}
sleep(1);
}
close(pipefd[0]);
} else {
close(pipefd[0]);
int number = 0;
while (running) {
print_process_info("Parent");
write(pipefd[1], &number, sizeof(number));
number++;
sleep(1);
int status;
pid_t result = waitpid(pid, &status, WNOHANG);
if (result == pid) {
std::cout << "Child stopped, father stopping" << std::endl;
running = false;
}
}
close(pipefd[1]);
}
std::cout << "finished" << std::endl;
return EXIT_SUCCESS;
}
/*
with ctr+c
Child received: 45
Parent | id: 2934
Parent | father id: 2610
Parent | groupd id: 2934
Child received: 46
^CReceived signal: 2
Stopping the program
Received signal: 2
Stopping the program
finished
Message during exit
finished
Message during exit
whil kill parent
Parent | id: 3396
Parent | father id: 2610
Parent | groupd id: 3396
Child received: 10
Parent | id: 3396
Parent | father id: 2610
Parent | groupd id: 3396
Child received: 11
Received signal: 15
Stopping the program
finished
Message during exit
with kill child
Child received: 41
Parent | id: 3644
Parent | father id: 2610
Parent | groupd id: 3644
Child received: 42
Received signal: 15
Stopping the program
finished
Message during exit
Child stopped, father stopping
finished
Message during exit
*/