Simulate a zombie process

What is a zombie process (zimbie)?

  
  A zombie process is created when a process exits and the parent process does not read the return code of the child process exit. When a process calls the exit command to end its own life, it does not actually destroy it, but leaves a data structure called a zombie process (the system calls exit, its function is to make the process exit, but It is also limited to turning a normal process into a zombie process, and cannot completely destroy it) and saves it in the process table in a terminated state.

Simulate a zombie process

write picture description here

Result presentation:
write picture description here

The dangers of zombie processes

  Since the end of the child process and the operation of the parent process are an asynchronous process, that is, the parent process can never predict when the child process will end. Then will it be too late to wait for the child process because the parent process is too busy, or do not know when the child process will end, and lose the state information at the end of the child process? No. Because UNIX provides a mechanism to ensure that as long as the parent process wants to know the status information when the child process ends, it can get it. This mechanism is: When each process exits, the kernel releases all the resources of the process, including open files, occupied memory, etc. However, some information is still reserved for it (including the process ID, the exit status, the termination status of the process, the amount of CPU time taken by the process, etc.). It is not released until the parent process takes the time through wait / waitpid. But this causes problems. If the process does not call wait / waitpid, the reserved information will not be released, and its process number will always be occupied, but The number of process numbers that the system can use is limited. If a large number of zombie processes are generated, the system will not be able to generate new processes because there is no available process number. This is the harm of zombie processes and should be avoided.

how to avoid

  
1. The parent process waits for the child process to end through functions such as wait and waitpid, which will cause the parent process to hang.
⒉ If the parent process is very busy, you can use the signal function to install a handler for SIGCHLD, because after the child process ends, the parent process will receive the signal, and you can call wait in the handler to recycle.
⒊ If the parent process does not care when the child process ends, it can use signal (SIGCHLD, SIG_IGN) to notify the kernel that it is not interested in the end of the child process, then after the child process ends, the kernel will recycle it and no longer send it to the parent process. Signal.
⒋ There are also some tricks, that is, fork twice, the parent process forks a child process, and then continues to work, the child process forks a grandchild process and then exits, then the grandchild process is taken over by init, and after the grandchild process ends, init will recycle. However, the recycling of the child process has to be done by itself.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324422000&siteId=291194637