Sunday, April 1, 2018

Look for Zombie Child Process

After forking a process, child and parent processes will execute and die.  If Child dies first, it will become a zombie process, unless it is handled.  Right now, I am not handling anything. Just doing a small extension to my previous program.


 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
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
 pid_t child_pid;
 
 printf ("This is the print before Fork\n");
 child_pid=fork();
 
 if (child_pid == 0) {
  //This is Child
  printf("\tpid=%d\n",getpid());
  printf("\tparentpid=%d\n",getppid());
  printf("\tChild ID=%d\n",child_pid);
  
  printf("\tChild Dying\n");
 } else {
  //This is Parent
  printf("pid=%d\n",getpid());
  printf("parentpid=%d\n",getppid());
  printf("Child ID=%d\n",child_pid);
  
  sleep(20);
 }
}

Note the sleep command in line 26. When the parent is sleeping (that is still processing), child is dead.


1
2
3
4
5
6
7
8
9
root@kali:/media/root/persistence# ./a.out 
This is the print before Fork
pid=5185
parentpid=3594
Child ID=5186
 pid=5186
 parentpid=5185
 Child ID=0
 Child Dying


1
2
3
4
5
6
7
root@kali:/media/root/persistence# ps -e -o pid,ppid,command
  PID  PPID COMMAND
.......
 4761     2 [kworker/3:0]
 4769  3594 ./a.out
 4770  4769 [a.out] <defunct>
 4771  2558 ps -e -o pid,ppid,command



In the process list, we see that the child process(pid # 4770) is listed as <defunct>.  It means it is Zombie Process.

According to Wikipedia, a zombie process or defunct process is a process that has completed execution (via the exit system call) but still has an entry in the process table: it is a process in the "Terminated state". This occurs for child processes, where the entry is still needed to allow the parent process to read its child's exit status: once the exit status is read via the wait system call, the zombie's entry is removed from the process table and it is said to be "reaped".
 

No comments:

Post a Comment