Consider the following two scenarios:
- The first type: the parent process exits before the child process.
When this happens, the child process becomes the orphan process. All orphan processes in the system will be adopted by the INIT process, and the process ID of the INIT process is always 1. When a process terminates within the system, the kernel checks to see if all processes are child processes of the terminating process, and if so, modifies the parent process ID to 1, which is adopted by the INIT process. The init process is guaranteed to wait for each child process to exit. This ensures that all processes within the system have a parent process. When an orphan process is adopted by the INIT process, the orphan process will be converted from the foreground process to the background process, which means that typing CTRL + C cannot terminate the process.
- Second: The child process exits before the parent process
When the child process exits before the parent process, if the parent process does not use the wait or Waitpid function to wait for the child process to exit, then the child process's kernel resource (PCB) will not be recycled, and this child process is called a zombie process. Thus, if there are a lot of zombie processes, then those that can not be recycled PCB will occupy the resources of the kernel, also known as a memory leak.
Process Control (VI)---orphan process and zombie process