1 Answers
#include <unistd.h>
#include <stdio.h>
int main() {
int ret;
printf("welcome..pid=%d\n", getpid());
ret = fork();
if (ret < 0) {
perror("fork");
exit(1);
}
if (ret == 0) {
printf("child--welcome,pid=%d,ppid=%d\n", getpid(), getppid());
exit(0);
} else // ret>0
{
printf("parent--hello,pid=%d,ppid=%d\n", getpid(), getppid());
printf("enter any char to terminate\n");
getchar(); // sleep(5);
// no waitpid, parent is not cleaning up child's PCB
}
return 0;
}
Notes
- A process got terminated, but not cleaned up by parent (no waitpid call) is known as zombie process (in zombie state)
- Typically exit will release all resources, except PCB/PD and entry in process table
- waitpid by parent is expected to clear these
- Too many zombie process causes process table to be full and failure of future fork request
- It’s adviced that parent should use waitpid, even blocking & exit status collection not required
Observations & Understanding
- Observing child in zombie state , ps command output (Symbol Z, defunct)
- Two phase termination of any process
- Running to Zombie, release all resources by exit system calls, except PCB and entry in table
- Zombie to Terminated, clean up PCB & process table entry by parent using waitpid
- What’ll happen to Zombie processes when parent terminates
- Problems with too many Zombie processes when parent doesn’t terminate for sometime.
- How to avoid zombies — use waitpid by parent even if not intrested in exit status of child