-
Notifications
You must be signed in to change notification settings - Fork 9
/
vfork_pids.c
106 lines (93 loc) · 1.98 KB
/
vfork_pids.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
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
/* Author: Vivek Prakash
* Date: Aug 1, 2011
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
extern int errno;
int main() {
pid_t pid[7];
int index = 0;
printf("In the parent process\n");
pid[0] = getpid();
pid_t id;
id = vfork();
if(id < 0)
printf("Error: %d\n", errno);
else if(id > 0) {
/* In the parent process */
pid[0] = getpid();
pid_t id1 = vfork();
if(id1 < 0)
printf("Error: %d\n", errno);
else if(id1 > 0) {
/* Back in the parent process */
}
else if (id1 == 0) {
printf("In the child process - 2\n");
pid[2] = getpid();
pid_t id4;
id4 = vfork();
if(id4 < 0)
printf("Error: %d\n", errno);
else if(id4 > 0) {
/* Back in the child process - 2 */
pid_t id5;
id5 = vfork();
if(id5 < 0)
printf("Error: %d\n", errno);
else if(id5 > 0) {
/* Back in the child process - 2 */
pid_t id6;
id6 = vfork();
if(id6 < 0)
printf("Error: %d\n", errno);
else if(id6 > 0) {
/* Back in the child process - 2 */
} else if(id6 == 0) {
printf("In the grand child process - 4\n");
pid[6] = getpid();
_exit(0);
}
}
else if(id5 == 0) {
printf("In the grand child process - 3\n");
pid[5] = getpid();
_exit(0);
}
}
_exit(0);
}
/* Print the pid array */
for(int i = 0 ; i < 7 ; ++i)
printf("%d\n", pid[i]);
} else if(id == 0) {
printf("In the child process - 1\n");
pid[1] = getpid();
pid_t id2;
id2 = vfork();
if(id2 < 0)
printf("Error: %d\n", errno);
else if(id2 > 0) {
/* Back in the child process - 1 */
pid_t id3;
id3 = vfork();
if(id3 < 0)
printf("Error: %d\n", errno);
else if(id3 > 0) {
/* Back in the child process - 1 */
}
else if(id3 == 0) {
printf("In the grand child process - 2\n");
pid[4] = getpid();
_exit(0);
}
}
else if (id2 == 0) {
printf("In the grand child process - 1\n");
pid[3] = getpid();
_exit(0);
}
_exit(0);
}
}