<!--[if!supportlists]-->ÿ<!--[endif]-->wait for the child process to end pid_t Waitpid (pid_t pid,int*stat_loc,intoptions) There is another function called wait, which is equivalent to Waitpid (-1, &status,0The Classic example of waitpid that you've seen so often is that you downloaded a software installer, a program that launches another rogue software installer at the end of the installation, and when B is installed, it tells you that all the installations were successful. A and B are in different processes, a how to start B and know that the B installation is complete? It is easy to start B with fork in a, and then use Waitpid (or wait) to wait for the end of B. Parameter PID: If greater than 0, indicates that the process number of the child process that the parent process needs to wait for is equal to 0, then any arbitrary group ID and the same child process as the parent process should be equal to-1, the waitpid and wait are the same when waiting for any child process (with multiple sub-processes, the end of any process, and the function will return). If less than-1, take its absolute value as the process number parameter of the child process that needs to wait Stat_loc: Indicates where the process state is stored when the process exits, and there are some specialized macro classes that calculate the status value based on that location, which can be referenced here. Parameter options: This parameter controls whether the function returns immediately, and it has three values:0, Wnohang (value 1), wuntraced (a value of 2), how many of these three values are somewhat confusing, one of the posts says: The options ' constants are not mutually exclusive, but rather are combined by bitwise OR operation. The state number of the process is limited, all the process state change possibility, is a limited number of elements of the set, the waitpid of the specified sub-process state changes, is bound to be a subset of this set, recorded as a. The options determine how to take the elements in a, by default (0only if A is not an empty set, it is returned, otherwise blocked. Wnohang tells Waitpid that even if a is an empty set, it does not hang, but returns immediately. Wuntraced tells Waitpid that if a contains a process stoped state, it will return immediately. If the child process is a trace, the return is understood even if the wuntraced parameter is not provided. #include<stdio.h>//For printf ()#include <unistd.h>//For Fork ()#include <sys/wait.h>//For wait ()#include <stdlib.h>//For exit_success intMain () {printf ("app start...\n"); printf ("Do something in main process\n"); Sleep (5); if(fork () = =0) {printf ("Do something in child process ... \ n"); Sleep (5); Exit (exit_success); printf ("This is not been executed\n"); } intstatus; Wait (&status); printf ("app end\n"); return 0;} We know that when the process is finished, most of the resources of the process will be recycled, such as freeing memory, closing descriptors, etc., but the structure of the process Struct_task still exist, at this time the process is equivalent to "The Soul is dead, the body remains", so called Zombie State, The existence of this struct has its meaning, because the process will keep some information in it before exiting, the parent process can get the struct in wait or waitpid and get the relevant information, the structure will be destroyed and the child process disappears completely.
Linux process Learning notes-waiting for child process to end