1 Basic Overview 1.1 orphan process and zombie process
After the parent process creates the child process, the ideal state is that the child process ends, the parent process reclaims the child process and frees the resources that the child process occupies, whereas in fact, the parent-child process is an asynchronous process, and the two who end first are not smooth, and can usually call the wait () or waitpid () statement through the parent process to wait for
Orphan process: The parent process after the end of a child process created based on the parent process (one or more) has not ended, at this time the child process is called the orphan process; The orphan process will be accepted by the INIT process (in addition to the parent process in the process tree), which means that the INIT process is responsible for completing the state collection of the orphan process In general, the PID of the Init process is 1, there is data to show that there are three forms of init, the PID is not 1.
Zombie process: In the Linux process state and transition relationship there is a process state is a zombie state (zombie), at this time the process is called a zombie process, when the child process is created using fork, the child process exits, While the parent process does not call wait or waitpid to get state information for the child process, the process description in the child process is still stored in the system, a process called a zombie process, sometimes called a dead process.
1.2 Orphan process and zombie process endangerment
Orphan process: The orphan process does not consume system resources, the parent process is recycled to process orphans, so the orphan process does not occupy system resources, and sometimes the process logic is designed using this principle of the orphan process.
Zombie Process: LUnix provides a mechanism by which the parent process obtains the state information of the child process, and at the time each process exits, the kernel frees all resources of the process, including open files, occupied memory, etc., but still retains some information about it (this information covers the process ID, Exit status The termination status of the process, runtime the amount of the CPU time taken by the process, etc.) until the parent process calls wait ()/Waitpid () to release,
When a process calls the Exit command to end its own life, it is not actually destroyed, but rather leaves behind a data structure called the zombie process (Zombie) (System call exit, which is the function of making the process exit, but only to turn a normal process into a zombie process, and cannot be completely destroyed). In the state of the Linux process, the zombie process is a very special one, it has abandoned almost all memory space, no executable code, and can not be scheduled, just keep a position in the process list, record the process's exit status and other information for other processes to collect, in addition, The zombie process no longer occupies any memory space. It needs its parent process to bury it, if his parent process does not install the SIGCHLD signal handler call wait or Waitpid () waits for the child process to end, and does not explicitly ignore the signal, then it remains zombie state, if the parent process is over, Then the init process will automatically take over the child process, and for it to corpse, it can still be cleared. But if the parent process is a loop and does not end, then the child process will remain zombie state, which is why there are sometimes many zombie processes in the system.
It is worth noting that:
All processes will have the process of becoming a zombie process, and each process will send its own process information to the parent process, waiting for the parent process to process, and this wait stage is the zombie process. The difference is that the normal parent process will soon dispose of the child process that has died, but the parent process does not receive the message, or when it is trapped in a dead loop, the zombie process is permanently suspended from the system and cannot be processed. The zombie process occupies a process ID number, but there is no way to release it, which is undoubtedly a hazard to the system.
UNIX provides a mechanism that can be obtained if the parent process wants to know the state information at the end of the child process. The mechanism is that when each process exits, the kernel frees all of the resources of the process, including open files, memory usage, and so on. However, it still retains certain information (including process ID, exit status of the termination status of the process, run time of the amount of the CPU taken by the Proces s, etc.). It is not released until the parent process passes wait/waitpid. However, this causes the problem, if the process does not call Wait/waitpid, then the reservation of the piece of information will not be released, its process number will always be occupied, but the system can use the process number is limited, if a large number of zombie process generated, The system cannot produce a new process because no process number is available. This is the threat to the zombie process and should be avoided.
But the orphan process differs from the zombie process in that it helps the parent process recycle the orphaned process because the parent process is dead. So the orphan process is actually not resource-intensive, because it is eventually reclaimed by the system. Does not occupy the ID as the zombie process, damaging the operating system.
The orphan process will be destroyed after the INIT process, and there is no harm, and the zombie process will always occupy the process number, the operating system is limited the number of processes will be affected.
A process uses fork to create a child process, and if the child process exits, and the parent process does not call wait or waitpid to get state information for the child process, the process descriptor of the child process is still stored in the system. This process is called a zombie process.
If we know about the Linux process state and the transition relationship, we should know that there is a state in the process so many states is a zombie state, that is, the process terminates into a zombie state, waiting to tell the parent process to terminate itself before it completely disappears. But if a process has been terminated, but its parent process has not yet acquired its state, Then this process is called a zombie process. The zombie process also consumes a certain amount of system resources, and also retains some profile information for the parent process to query the state of the child process to provide the information the parent process wants. Once the parent process gets the desired information, the zombie process ends.
1, in general, the child process is created by the parent process, and the child process and the parent process exit is not sequential, neither knows who exits first. Normally the parent process ends up calling wait or the Waitpid function waits for the child process to complete before exiting, and once the parent process does not wait for a direct exit, the remaining child processes are received by the Init (pid=1) process and orphaned. (The process tree will have a parent process in addition to init).
2. If the child process exits first, the parent process is not finished and the wait is not called or the Waitpid function gets the state information of the child process, the remaining state information (task_struct structure and small amount of resource information) of the child process becomes a zombie process.
Child process death requires the parent process to process, then it means that the normal process should be the child process before the parent process dies. When the parent process dies before the child process, the child process dies without the parent process, and the child process of the death is the orphan process.
But the orphan process differs from the zombie process in that it helps the parent process recycle the orphaned process because the parent process is dead. So the orphan process is actually not resource-intensive, because it is eventually reclaimed by the system. Does not occupy the ID as the zombie process, damaging the operating system.
The parent process is the terminating state of the child process called by the Wait () or waitpid () system call.
The child process becomes a zombie process when the child process ends before the parent process, and the parent process does not reclaim the child process and frees the resources that the child process occupies. If the parent process exits first
, the child process is taken over by Init, and Init reclaims the related resources it occupies after the child process exits.
Python Learning notes-orphan process and zombie process