Linux zombie process and its solution

  

Linux zombie process and its solution

1. Cause:

In UNIX system, a process is over, but his The parent process does not wait (call wait /waitpid) him, then he will become a zombie process. Use the ps command to view its flag with defunct. A zombie process is a process that has already died, but it still occupies a slot in the processes table.

But if the parent process of the process has ended first, then the process will not become a zombie process. Because each process ends, the system scans all processes running on the current system to see if any process is the child of the process that just ended. If it is, the Init process takes over and becomes him. The parent process, thus ensuring that each process will have a parent process. The Init process will automatically wait for its child processes, so all processes taken over by Init will not become zombie processes.

2. Principle analysis:

Each Unix process has an entry in the process table. All information used by the core process to execute the process is stored at the entry point. . When you use the ps command to view the process information in the system, you see the relevant data in the process table. When a new process is created with the fork() system call, the core process assigns an entry point to the new process in the process table, and then stores the relevant information in the process table corresponding to the entry point. One of these messages is the ID of its parent process.

The end of the child process and the running of the parent process is an asynchronous process, that is, the parent process can never predict when the child process ends. So, is it because the parent process is too busy to wait for the child process, or does not know when the child process ends, and lose the state information at the end of the child process? will not. Because UNIX provides a mechanism to ensure that as long as the parent process wants to know the state information at the end of the child process, you can get it. This mechanism is: When the child process finishes its own life cycle, it will execute the exit() system call, and the kernel releases all resources of the process, including open files, occupied memory, and so on. However, it still retains certain information (including the process ID, the exit code, the exit status of the process, the amount of CPU time taken by the process, etc.), and the data is retained until the system. Pass it to its parent process until the parent process is fetched by wait /waitpid.

3. Solution:

(1) The parent process waits for the child process to end with functions such as wait and waitpid, which causes the parent process to hang.

Executing the wait() or waitpid() system call, the child process will immediately return its data in the process table to the parent process after termination, and the system will immediately delete the entry point. In this case, the defunct process will not be generated.

(2) If the parent process is busy, you can use the signal function to install the handler for SIGCHLD. After the child process ends, the parent process will receive the signal, and call wait recycling in the handler.

(3) If the parent process does not care when the child process ends, you can use signal (SIGCLD, SIG_IGN) or signal (SIGCHLD, SIG_IGN) to inform the kernel that you are not interested in the end of the child process, then the child After the process ends, the kernel will reclaim and no longer send a signal to the parent process

(4) 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 Sun process is taken over by init, and after the grand process is over, init will be recycled. However, the recycling of the child process has to be done by itself.



Copyright © Windows knowledge All Rights Reserved