When the parent process calls the wait and WAITPID functions to get the aborted state of the child process, there are generally 3 scenarios:
1, if all of its child processes are still running, then blocking;
2. If a child process is terminated and is waiting for the parent process to obtain its termination state, the terminating state of the child process is returned immediately;
3. If there are no child processes, the return is terminated immediately.
If the parent process receives a SIGCHLD signal (when a process is normal or abnormally terminated), the kernel sends a SIGCHLD signal to its parent process, and the call to Wait,wait returns immediately.
The function prototype is as follows:
The difference between the two functions is as follows:
1. Before a child process terminates, wait causes its caller to block, and Waitpid has an option (int options) that makes the caller not block;
2. Waitpid does not wait for the first aborted child process after its invocation, it has several options to control the process it waits for.
The function of PID parameter in Waitpid function is explained as follows:
The Waitpid function returns the ID of the terminating subprocess and holds the terminating state of the subprocess in the storage unit pointed to by Statloc.
3. For wait, the only error is that the calling process has no child processes;
However, for Waitpid, if the specified process or process group does not exist, or if the process specified by the parameter PID is not a subprocess of the calling process, there may be an error.