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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
| #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <fcntl.h>
#include <wait.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#define PROC_COUNT (9)
#define STARTING_PROC_ID (1)
#define MAX_CHILDS_COUNT (4)
#define MAX_USR_COUNT (101)
//#define RECV
#define MICRO
//#define DEBUG_PIDS
//#define DEBUG_HANDS
//#define DEBUG_SIGS
const unsigned char CHILDS_COUNT[PROC_COUNT] =
{
//количество дочерних узлов
/* 0 1 2 3 4 5 6 7 8 */
1, 3, 2, 0, 0, 0, 1, 1, 0
};
const unsigned char CHILDS_IDS[PROC_COUNT][MAX_CHILDS_COUNT] =
{
//значения таблицы 1
{1}, /* 0 */
{2,3,4}, /* 1 */
{5,6}, /* 2 */
{0}, /* 3 */
{0}, /* 4 */
{0}, /* 5 */
{7}, /* 6 */
{8}, /* 7 */
{0} /* 8 */
};
const unsigned char GROUP_TYPE[PROC_COUNT] =
{
//группы узлов
/* 0 1 2 3 4 5 6 7 8 */
0, 0, 1, 1, 1, 2, 1, 1, 1
};
const char SEND_TO[PROC_COUNT] =
{
//последовательность передачи сигналов (с минусом - сигнал отправляется группе узлов)
/* 0, 1, 2, 3, 4, 5, 6, 7, 8 */
0, -1, -2, 0, 0, 0, 7, 8, 1
};
const int SEND_SIGNALS[PROC_COUNT] =
{
//значения таблицы 2
0, /* 0 */
SIGUSR1, /* 1 */
SIGUSR2, /* 2 */
0, /* 3 */
0, /* 4 */
0, /* 5 */
SIGUSR1, /* 6 */
SIGUSR1, /* 7 */
SIGUSR2 /* 8 */
};
void sig_handler(int signum);
void set_sig_handler(void (*handler)(int), int sig_no, int flags);
char *exec_name = NULL;
void print_error_exit(const char *s_name, const char *msg, const int proc_num);
int proc_id = 0;
void forker(int curr_number, int childs_count);
void kill_wait_for_children();
void wait_for_children();
pid_t *pids_list = NULL;
char *tmp_file_name = "/tmp/pids.log";
int main(int argc, char *argv[])
{
exec_name = basename(argv[0]);
pids_list = (pid_t*)mmap(pids_list, (2*PROC_COUNT)*sizeof(pid_t), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
int i = 0; // initialize pids list with zeros [1]
for (i = 0; i < 2*PROC_COUNT; ++i) {
pids_list[i] = 0;
}
forker(0, CHILDS_COUNT[0]); // create processes tree [2]
set_sig_handler(&kill_wait_for_children, SIGTERM, 0);
if (proc_id == 0) { // main process waits [3]
wait_for_children();
munmap(pids_list, (2*PROC_COUNT)*sizeof(pid_t));
return 0;
}
on_exit(&wait_for_children, NULL);
pids_list[proc_id] = getpid(); // save pid to list
#ifdef DEBUG_PIDS
printf("%d\tpid: %d\tppid: %d\tpgid: %d\n", proc_id, getpid(), getppid(), getpgid(0));
fflush(stdout);
#endif
if (proc_id == STARTING_PROC_ID) { // starter waits for all pids to be available [3]
do{
for (i = 1; (i <= PROC_COUNT) && (pids_list[i] != 0); ++i) {
if (pids_list[i] == -1) {
print_error_exit(exec_name, "Error: not all processes forked or initialized.\nHalting.", 0);
exit(1);
}
}
// printf("%d\t", i);
} while (i < PROC_COUNT);
#ifdef DEBUG_PIDS
printf("All pids are set!\n");
for (i = 0; (i < 2*PROC_COUNT); ++i) {
printf("%d - %d\n", i, pids_list[i]);
fflush(stdout);
}
#endif
FILE *tmp = fopen(tmp_file_name, "wt");
if (tmp == NULL) {
print_error_exit(exec_name, "Can't create temp file", 0);
}
for (i = 1; i < PROC_COUNT; ++i) {
fprintf(tmp, "%d\n", pids_list[i]);
}
fclose(tmp);
pids_list[0] = 1; // all pids are set
set_sig_handler(&sig_handler, 0, 0);
do{
for (i = 1+PROC_COUNT; (i < 2*PROC_COUNT) && (pids_list[i] != 0); ++i) {
if (pids_list[i] == -1) {
print_error_exit(exec_name, "Error: not all processes forked or initialized.\nHalting.", 0);
exit(1);
}
}
// printf("%d\t", i);
} while (i < 2*PROC_COUNT);
for (i = PROC_COUNT+1; i < 2*PROC_COUNT; ++i) { /* reset flags */
pids_list[i] = 0;
}
#ifdef DEBUG_HANDS
printf("All handlers are set!\n");
for (i = 0; (i < 2*PROC_COUNT); ++i) {
printf("%d - %d\n", i, pids_list[i]);
fflush(stdout);
}
puts("==================================");
#endif
sig_handler(0); // start signal-flow
} else { // other processes
do {
// wait for all pids to be written
} while (pids_list[0] == 0);
set_sig_handler(&sig_handler, 0, 0);
}
while (1) {
pause(); // start cycle
}
return 0;
} /* main */
void print_error_exit(const char *s_name, const char *msg, const int proc_num) {
fprintf(stderr, "%s: %s %d\n", s_name, msg, proc_num);
fflush(stderr);
pids_list[proc_num] = -1;
exit(1);
} /* print_error */
void wait_for_children() {
int i = CHILDS_COUNT[proc_id];
while (i > 0) {
wait(NULL);
--i;
}
} /* wait_for_children */
/* ====================================================== */
long long current_time() {
struct timeval time;
gettimeofday(&time, NULL);
#ifdef MICRO
return time.tv_usec;
#else
return time.tv_usec%1000;
#endif
} /* current_time */
/* U1, U2 */
volatile int usr_recv[2] = {0, 0};
volatile int usr_amount[2][2] =
{
/* r, s */
{0, 0}, /* SIGUSR1 */
{0, 0} /* SIGUSR2 */
};
void kill_wait_for_children() {
int i = 0;
for (i = 0; i < CHILDS_COUNT[proc_id]; ++i) {
#ifdef DEBUG_PIDS
printf("sigint -> %d\n", CHILDS_IDS[proc_id][i]);
fflush(stdout);
#endif
kill(pids_list[CHILDS_IDS[proc_id][i]], SIGTERM);
}
wait_for_children();
if (proc_id != 0)
#ifdef DEBUG_SIGS
printf("%lld %d was terminated after %d SIGUSR1 and %d SIGUSR2\n",
current_time(), proc_id, usr_amount[0][1], usr_amount[1][1]);
#else
printf("%d %d %d завершил работу после %s %d SIGUSR1 и %d SIGUSR2\n", proc_id,
#ifndef RECV
getpid(), getppid(), "отправки ", usr_amount[0][1], usr_amount[1][1]);
#else
getpid(), getppid(), "получения", usr_amount[0][0], usr_amount[1][0]);
#endif
fflush(stdout);
#endif
exit(0);
} /* kill_wait_for_children */
void sig_handler(int signum) {
if (signum == SIGUSR1) {
signum = 0;
} else if (signum == SIGUSR2) {
signum = 1;
} else {
signum = -1;
}
if (signum != -1) {
++usr_amount[signum][0];
++usr_recv[signum];
#ifdef DEBUG_SIGS
printf("%lld %d received %s%d\n", current_time(), proc_id,
"USR", signum+1 );
#else
printf("%d %d %d получил %s%d %lld\n", proc_id, getpid(), getppid(),
"USR", signum+1, current_time() );
#endif
fflush(stdout);
#ifdef DEBUG_SIGS
printf("%lld %d has %d:%d\n", current_time(), proc_id, usr_recv[0], usr_recv[1]);
fflush(stdout);
#endif
if (proc_id == 1) {
if (usr_amount[0][0] + usr_amount[1][0] == MAX_USR_COUNT) {
kill_wait_for_children();
}
}
usr_recv[0] = usr_recv[1] = 0;
}
if(SEND_SIGNALS[proc_id]==0) return;
char to = SEND_TO[proc_id];
if (to != 0) {
signum = ( (SEND_SIGNALS[proc_id] == SIGUSR1) ? 1 : 2);
++usr_amount[signum-1][1];
}
#ifdef DEBUG_SIGS
printf("%lld %d sent %s%d\n", current_time(), proc_id,
"USR", signum);
#else
printf("%d %d %d послал %s%d %lld\n", proc_id, getpid(), getppid(),
"USR", signum, current_time());
#endif
fflush(stdout);
if (to > 0) {
kill(pids_list[to], SEND_SIGNALS[proc_id]);
} else if (to < 0) {
kill(-getpgid(pids_list[-to]), SEND_SIGNALS[proc_id]);
} else {
return;
}
} /* handler */
void set_sig_handler(void (*handler)(int), int sig_no, int flags) {
struct sigaction sa; // set sighandler [4]
sigset_t block_mask;
sigemptyset(&block_mask);
sigaddset(&block_mask, SIGUSR1);
sigaddset(&block_mask, SIGUSR2);
sa.sa_mask = block_mask;
sa.sa_flags = flags;
sa.sa_handler = handler;
if (sig_no != 0) {
sigaction(sig_no, &sa, 0);
return;
}
int i = 0;
for (i = 0; i < PROC_COUNT; ++i) {
char to = SEND_TO[i];
/* someone sends me a signal: */
if ( ( (to > 0) && (to == proc_id) ) || /* <- directly */
( (to < 0) && (getpgid(pids_list[-to]) == getpgid(0)) ) ) { /* <- or through the group */
if (SEND_SIGNALS[i] != 0) { // signal is really sent
if (sigaction(SEND_SIGNALS[i], &sa, 0) == -1) {
print_error_exit(exec_name, "Can't set sighandler!", proc_id);
}
#ifdef DEBUG_HANDS
printf("%d) will receive a signal %d from %d\n", proc_id, SEND_SIGNALS[i], i);
fflush(stdout);
#endif
}
}
} /* for */
pids_list[proc_id + PROC_COUNT] = 1;
} /* set_sig_handler */
void forker(int curr_number, int childs_count) {
pid_t pid = 0;
int i = 0;
for (i = 0; i < childs_count; ++i) {
int chld_id = CHILDS_IDS[curr_number][i];
if ( (pid = fork() ) == -1) {
print_error_exit(exec_name, "Can't fork!", chld_id);
} else if (pid == 0) { /* child */
proc_id = chld_id;
if (CHILDS_COUNT[proc_id] != 0) {
forker(proc_id, CHILDS_COUNT[proc_id]); // fork children
}
break;
} else { // pid != 0 (=> parent)
static int prev_chld_grp = 0;
int grp_type = GROUP_TYPE[chld_id];
if (grp_type == 0) {
if (setpgid(pid, pid) == -1) {
print_error_exit(exec_name, "Can't set group", chld_id);
} else {
prev_chld_grp = pid;
}
} else if (grp_type == 1) {
if (setpgid(pid, getpgid(pids_list[1])) == -1) {
print_error_exit(exec_name, "Can't set group", chld_id);
}
} else if (grp_type == 2) {
if (setpgid(pid, prev_chld_grp) == -1) {
print_error_exit(exec_name, "Can't set group", chld_id);
}
}
} // parent branch
} // for
} |