-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforktree.c
46 lines (45 loc) · 1.25 KB
/
forktree.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
#include <stdio.h>
#include <unistd.h>
void main()
{
int pId1;
pId1 = fork();
if (pId1 < 0)
{
printf("Process creation is Unsuccessful!");
}
// The return value is 0 for a child process
else if (pId1 == 0)
{
printf("Child process:");
printf("\nChild : Child’s PID: %d", getpid());
printf("\nChild : Parent’s PID: %d\n", getppid());
int pId2;
pId2 = fork();
if (pId2 < 0)
{
printf("Process creation is Unsuccessful!");
}
// The return value is 0 for a child process
else if (pId2 == 0)
{
printf("Child process:");
printf("\nChild : Child’s PID: %d", getpid());
printf("\nChild : Parent’s PID: %d\n", getppid());
}
// The return value is positive for a parent process
else if (pId2 > 0)
{
printf("Parent process:");
printf("\nParent : Parent’s PID: %d", getpid());
printf("\nParent : Child’s PID: %d\n", pId2);
}
}
// The return value is positive for a parent process
else if (pId1 > 0)
{
printf("Parent process:");
printf("\nParent : Parent’s PID: %d", getpid());
printf("\nParent : Child’s PID: %d\n", pId1);
}
}