-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignal_fork_ex.c
45 lines (37 loc) · 1.08 KB
/
signal_fork_ex.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
#include <unistd.h>
#include <stdio.h>
int main()
{
// Creating first child
int n1 = fork();
// Creating second child. First child also executes this line and creates grandchild.
int n2 = fork();
if (n1 > 0 && n2 > 0)
{
printf("parent\n");
printf("%d %d \n", n1, n2);
printf(" my id is %d \n", getpid());
printf(" my parentid is %d \n", getppid());
}
else if (n1 == 0 && n2 > 0)
{
printf("\nFirst child\n");
printf("%d %d \n", n1, n2);
printf(" my id is %d \n", getpid());
printf(" my parentid is %d \n", getppid());
}
else if (n1 > 0 && n2 == 0)
{
printf("\nsecond child\n");
printf("%d %d \n", n1, n2);
printf(" my id is %d \n", getpid());
printf(" my parentid is %d \n", getppid());
}
else {
printf("\nthird child\n");
printf("%d %d \n", n1, n2);
printf(" my id is %d \n", getpid());
printf(" my parentid is %d \n", getppid());
}
return 0;
}