linux wait与waitpid函数的深入分析(国外英文资料)
linux wait与waitpid函数的深入分析(国外英文资料)
The in-depth analysis of Linux wait and waitpid functions
There are already 2050 reading of the 2010-9-3020:1| personal category: the LINUX system | keyword: waitpid LINUX function wait subprocess fork zombie
A) system calls wait
1) overview
The wait function's prototype is: pid_t wait (int * status)
When the process exits, it send a SIGCHLD signal to the parent process, always ignore the SIGCHLD signal by default, the process state remain in memory until the parent process USES wait function status information collection, will clear the information.
Waiting for a subprocess to terminate with wait is called the recovery process.
When the parent forget to use the wait () function to wait for terminated the child, the child will enter a state of no parent, the child process is a zombie process.
Wait () and fork () form a complete set, if you are using the fork () before calling wait (), wait () returns a value of 1, under normal circumstances the wait () return values for PID of the child process.
If the parent process is terminated first, the child process will continue normally, except that it will be inherited by the init process (PID 1), which is captured by the init process when the child process terminates.
2) the formation of zombie processes
The source program is as follows:
# include < sys/types. H >
# include < sys/wait. H >
# include < unistd. H >
# include < stdlib. H >
int
The main ()
{
Pid_t PC, pr;
PC = fork ();
If (PC < 0)
Printf (" error ocurred! \ n ");
The else
If (PC = 0)
This is child process with pid of % d \ n, getpid ());
}
The else {
Sleep (20);
This is partent with pid of % d \ n, getpid ());
}
The exit (0);
}
C compiler zombie.
GCC zombie. C - o zombie
Run the generated program
. / zombie
Also check the process status of the zombie
Ps - C zombie - o ppid, pid, stat, CMD
PPID PID STAT CMD
24233 26056 S +. / zombie
26056 26057 Z + [zombie] < defunct >
After 20 seconds, see the results of the program, as follow