At the end of the Linux neutron process, if the function of the wait series is not called, it becomes zombie. The kernel maintains part of the zombie process's information (PID, end State, resource utilization information), which is used by the parent process to then obtain the child process information. If the calling parent process terminates, the child process that is in a zombie state is discarded (init ()-Automatically calls wait).
#include <sys/types.h>
#include <sys/wait.h>
pid_t Wait (int *stauts);
pid_t waitpid (pid_t pid,int*stauts,int options);
These system calls are used to get child process state changes and state information. The change of state can be considered as: (1) The child process terminates, (2) The child process is signaled, and the child process is awakened by the signal.
These system J calls return immediately when the child process state changes. Otherwise it will block until the child process state changes or the signal handler (signal handler) aborts the child process.
The wait () system call will block the process until a child process terminates. Wait (&status) is equivalent to:
Waitpid ( -1,&status,0);
The Waitpid () system call will block the process until the specified child process state changes. By default, Waitpid () blocks to a child process termination, but can change the wait target through the third parameter options.
PID of the first parameter:
<-1 indicates that the waiting sub-process group number is PID absolute.
-1 indicates waiting for any child process.
0 means that waiting for the process group number equals any child processes that call the process.
>0 represents a child process waiting for the process number to be PID.
The options can have a value of 0 or the following constants:
Wnohang if the PID-specified child process does not end, the Waitpid () function returns 0, not waiting. If it ends, the ID of the child process is returned.
Wuntraced also returns when the child process aborts
Wcontinued Abort child process is also returned when Sigcont wakes
After the child process is finished, the status information is stored in the second parameter status, which is an int type pointer, which can be judged by the following macro:
wifexited (status) If the child process terminates normally, the returned state is true.
Wexitstatus (status) Gets the end code returned by the child process exit (), typically using wifexited to determine whether the macro ends properly before it can be used.
wifsignaled (status) If the child process is signaled signaled the status returned is true.
Wtermsig (status) returns the signal value that caused the child process to terminate, and it needs to be used if the wifsignaled return value is true.
Wcoredump (status) If the child process produces a core dump return true, this also requires that the wifsignaled return value be true for use. Not all UNIX systems implement this macro and need to work with
#ifdef Wcoredump
...
#endif
wifstopped (status) If the child process does not terminate, but stops and can be re-executed, the macro returns TRUE. This situation only occurs when the wuntraced option is used in the Waitpid call.
Wstopsig (status) If Wifstopped returns True, the macro returns the signal (signal) value that causes the child process to stop.
Waitid ():
The Waitid () system call is used to obtain a finer-grained change in the state of a child process, succeeds or specifies Wnohang, but returns 0 when the state of the child process referred to by the ID has not changed, and an error returns-1.
int Waitid (idtype_t idtype,id_t id,siginfo_t*infop,int options);
The Idtype and ID specify the waiting child process or multiple sub-processes, as follows:
Idtype=p_pid: The child process waiting for the specified process number.
Idtype=p_pgid: Waits for all child processes that specify the process group number.
Idtype==p_all: Waits for all child processes.
The options can be of the following or combination: wexited: Wait for those child processes that have been terminated wstopped: Wait for those child processes that are paused by the signal wcontinued: Wait for those child processes that are restarted by Sigcont Wnohang: wnowait in the same wiatpid: returned from a child process that is in a "waiting state", but later wait can also get the state of the child process
If INFOP is not NULL, the structure will be filled in when the Waitid returns successfully, and we can get the information we are interested in from this structure, including: Si_pid: Process ID of the subprocess si_uid: The real user ID of the child process Si_signo: Always set to Sigchid Si_status: either the return state of the child process or the signal that caused the child process state to change, Si_code explains how to interpret the domain