-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple-forks.c
75 lines (66 loc) · 2.14 KB
/
multiple-forks.c
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
// contains all the errors
#include <errno.h>
/*
________________ ________________
| | | z |
| id1 = x | | id1 = x |
| id2 = z |----------| id2 = 0 |
|_______________| |_______________|
|
|
_______|________
| x |
| id1 = x |
| id2 = z |
|_______________|
|
|
_______|________
| y |
| id1 = 0 |
| id2 = 0 |
|_______________|
*/
int main(int argc, char *argv[])
{
// twice fork results in four processes
int id1 = fork();
int id2 = fork();
if (id1 == 0)
{
if (id2 == 0)
{
// child of child process
printf("Process y. ID: %d. Parent ID: %d\n", getpid(), getppid());
}
else
{
printf("Process x. ID: %d. Parent ID: %d\n", getpid(), getppid());
}
}
else
{
if (id2 == 0)
{
printf("Process z. ID: %d. Parent ID: %d\n", getpid(), getppid());
}
else
{
printf("Parent process. ID: %d. Parent ID: %d\n", getpid(), getppid());
}
}
// waiting here for the child processes becomes hard
// as wait only waits for one process
// waiting for one process and terminating of the parent leads to the other children turning into orphans
// and here we have three
while (wait(NULL) != -1 /*if theres nothing to wait for*/ || errno != ECHILD /*No child processes*/)
{
// this should be printed three times on the screen as we have three child processes the parent has to wait for
printf("Waited for a child to finish\n");
}
return 0;
}