-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_Zombie_Process.c
105 lines (89 loc) · 2.51 KB
/
10_Zombie_Process.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
// Q. Create an zombie process
/*
Note:
Zombie Process:
A zombie is a process which has terminated but its entry still exists in the process table
until the parent terminates normally or calls wait().
Suppose you create a child process and the child finishes before the parent process does.
If you run the ps command before the parent gets terminated the output of ps will show
the entry of a zombie process(denoted by defunct).
This happens because the child is no longer active but its exit code needs to be stored in case
the parent subsequently calls wait.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
int f;
printf("Before fork!\n");
f = fork();
// The creation of the process was unsuccessful
if (f < 0)
{
printf("Error occurred!\n");
}
// Child process
else if (f == 0)
{
printf("This is Child Process!\n");
printf("Child: Child Process pid: %d\n", getpid());
printf("Child: Parent Process pid: %d\n", getppid());
}
// Parent process
else
{
// Helps to avoid zombie process creation situation
wait(NULL);
// Parent process will be in sleep for 5 secs
// Time needed to run ps command
sleep(5);
printf("This is Parent Process!\n");
printf("Parent: Parent Process pid: %d\n", getpid());
printf("Parent: Child Process pid: %d\n", f);
}
}
/*
Output: [when Zombie process is created - wait() not called]
s4shibam@SHIBAM:~/OS$ gcc 10_Zombie_Process.c
s4shibam@SHIBAM:~/OS$ ./a.out &
[1] 2617
s4shibam@SHIBAM:~/OS$ Before fork!
This is Child Process!
Child: Child Process pid: 2618
Child: Parent Process pid: 2617
ps
PID TTY TIME CMD
2202 tty1 00:00:00 bash
2617 tty1 00:00:00 a.out
2618 tty1 00:00:00 a.out <defunct>
2619 tty1 00:00:00 ps
s4shibam@SHIBAM:~/OS$ This is Parent Process!
Parent: Parent Process pid: 2617
Parent: Child Process pid: 2618
^C
[1]+ Done ./a.out
s4shibam@SHIBAM:~/OS$
** <defunct> denotes the Zombie Process
*/
/*
Output: [after avoiding Zombie process - wait() called]
s4shibam@SHIBAM:~/OS$ gcc 10_Zombie_Process.c
s4shibam@SHIBAM:~/OS$ ./a.out &
[1] 2658
s4shibam@SHIBAM:~/OS$ Before fork!
This is Child Process!
Child: Child Process pid: 2659
Child: Parent Process pid: 2658
ps
PID TTY TIME CMD
2202 tty1 00:00:00 bash
2658 tty1 00:00:00 a.out
2660 tty1 00:00:00 ps
s4shibam@SHIBAM:~/OS$ This is Parent Process!
Parent: Parent Process pid: 2658
Parent: Child Process pid: 2659
^C
[1]+ Done ./a.out
s4shibam@SHIBAM:~/OS$
*/