First, zombie process
When the child process exits, the kernel sends a SIGCHLD signal to the parent process, and the child process exits as an asynchronous event (the child process terminates at any time the parent process runs)
When a child process exits, the kernel leaves the subprocess as a zombie, a process called a zombie process that retains only the smallest kernel data structure so that the parent process queries the exit state of the child process.
The parent process can query the exit state of a subprocess using the Wait/waitpid function.
Second, how to avoid the zombie process
When a child process finishes running, its association with its parent process is persisted until the parent process has also finished running normally or the parent process has called wait/waitpid to terminate.
The data item that represents the subprocess in the process table is not immediately released, and although it is no longer active, the subprocess remains in the system because its exit code also needs to be saved for subsequent Wait/waitpid calls in the parent process. It will be called a "zombie process".
Call wait or Waitpid function to query the child process exit state, and this method parent process is suspended (Waitpid can be set to not hang).
If you do not want the parent process to suspend, you can add a statement to the parent process: signal (sigchld,sig_ign), which indicates that the parent process ignores the SIGCHLD signal, which is sent to the parent process when the child process exits. You can also not ignore the SIGCHLD signal and receive the call Wait/waitpid in the signal processing function.
The common method in operation is to kill the parent process, which will be taken over by the Init process to clean up the state of the child process.
Third, wait function
Header Files <sys/types.h> and <sys/wait.h>
function function: When we start a process with fork, the child process has its own life and will run independently. Sometimes, we need to know whether a subprocess is over, and we can arrange the parent process through wait by the end of the child process.
Function prototypes
pid_t Wait (int *status)
function arguments
Status: This parameter will get you the information you wait for the child process
return value:
Successfully wait for child process function to return ID of wait child process
The wait system call causes the parent process to suspend execution until one of its child processes ends.
Returns the PID of the subprocess, which is usually the end of the child process
State information allows the parent process to determine the exit state of the subprocess, that is, the value returned from the main function of the child process, or the exit code of the exit statement in the child process.
If status is not a null pointer, state information is written to the location it points to
The exit status of a subprocess can be obtained by using the following macro definitions
wifexited (status) Returns a non-0 value if the child process ends normally
Wexitstatus (status) Returns the child process exit code if the wifexited is Non-zero
The wifsignaled (status) subprocess terminates because of a capture signal and returns a value other than 0
Wtermsig (status) if wifsignaled Non-zero, return signal code
wifstopped (status) Returns a non-0 value if the child process is paused
Wstopsig (status) If wifstopped Non-zero, returns a signal code