-
Notifications
You must be signed in to change notification settings - Fork 1
/
flow.h
66 lines (60 loc) · 1.26 KB
/
flow.h
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
#ifndef MASSDNS_FLOW_H
#define MASSDNS_FLOW_H
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
static void kill_process_group(int sig)
{
static int received_termination = 0;
if(received_termination)
{
return;
}
received_termination = 1;
kill(0, sig);
exit(0);
}
static void handle_termination()
{
signal(SIGINT, kill_process_group);
signal(SIGTERM, kill_process_group);
}
// times is the number of resulting processes, i.e. if times is two, the process will fork once
size_t split_process(size_t times, pid_t *pids)
{
if(pids != NULL)
{
pids[0] = getpid();
}
for (size_t i = 0; i < times - 1; i++)
{
pid_t child = fork();
switch (child)
{
case -1:
{
perror("Failed to fork");
exit(EXIT_FAILURE);
}
case 0:
{
handle_termination();
return i + 1;
}
default:
if(pids != NULL)
{
pids[i + 1] = child;
}
break;
}
}
if(times > 1)
{
handle_termination();
}
return 0;
}
#endif