Refreshing your memory by picking few points from Zombie Process
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"
I included few things in my program, and started observing.
In the child section,
When the child is sleeping, 'ps' as usual shows parent and child process.
After child process is terminated and if we look at ps. Child would have been terminated and only parent process remains. There is no defunct process. We avoided zombie process as wait was called to retrieve the status of process.
At the end, here is the output of the program.
Observe the return value at line 12. Wait function returns the process ID of the child that got terminated.
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"
I included few things in my program, and started observing.
In the child section,
- I included sleep to verify if the child process is created.
- I am waiting to for the child to exit using ret
- Sleeping to verify if the child remained defunct process
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 | #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.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 is sleeping. Verify ps for child process list\n"); sleep(10); printf("\tChild wokeup and is terminated\n"); } else { //This is Parent printf("pid=%d\n",getpid()); printf("parentpid=%d\n",getppid()); printf("Child ID=%d\n",child_pid); int status=0,ret; printf("Waiting for the Child to terminate\n"); ret = wait(&status); printf("Wait is over. Return value ret=%d\n",ret); printf("Parent Sleeping. Verify ps for child defunc status\n"); sleep(20); printf("Parent Terminated\n"); } } |
When the child is sleeping, 'ps' as usual shows parent and child process.
1 2 3 4 5 6 7 8 9 | root@kali:/media/root/persistence# ./a.out This is the print before Fork pid=3088 pid=3089 parentpid=3088 parentpid=2797 Child ID=0 Child ID=3089 Child is sleeping. Verify ps for child process list |
1 2 3 4 5 6 | root@kali:/media/root/persistence# ps -e -o pid,ppid,cmd | tail -5 3080 2 [kworker/0:2] 3088 2797 ./a.out 3089 3088 ./a.out 3090 3020 ps -e -o pid,ppid,cmd 3091 3020 tail -5 |
After child process is terminated and if we look at ps. Child would have been terminated and only parent process remains. There is no defunct process. We avoided zombie process as wait was called to retrieve the status of process.
1 2 3 4 5 6 7 8 9 10 11 12 13 | root@kali:/media/root/persistence# ./a.out This is the print before Fork pid=3088 pid=3089 parentpid=3088 parentpid=2797 Child ID=0 Child ID=3089 Child is sleeping. Verify ps for child process list Waiting for the Child to terminate Child wokeup and is terminated Wait is over. Return value ret=3089 Parent Sleeping. Verify ps for child defunc status |
1 2 3 4 5 6 | root@kali:/media/root/persistence# ps -e -o pid,ppid,cmd | tail -5 3043 2 [kworker/u8:3] 3080 2 [kworker/0:2] 3088 2797 ./a.out 3092 3020 ps -e -o pid,ppid,cmd 3093 3020 tail -5 |
At the end, here is the output of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | root@kali:/media/root/persistence# ./a.out This is the print before Fork pid=3088 pid=3089 parentpid=3088 parentpid=2797 Child ID=0 Child ID=3089 Child is sleeping. Verify ps for child process list Waiting for the Child to terminate Child wokeup and is terminated Wait is over. Return value ret=3089 Parent Sleeping. Verify ps for child defunc status Parent Terminated |
Observe the return value at line 12. Wait function returns the process ID of the child that got terminated.
No comments:
Post a Comment