Last Month's article, php multi-process pcntl Learning (a) is now found not complete, because although the closure of the child process, but did not reclaim the child process, simply said that when the child process than the parent process first exit, and the parent process does not do any processing, the child process will become a zombie process.
1<?PHP2 $ppid=Getmygid();3 $pid=pcntl_fork ();4 if($pid= =-1){5 die(' Fork Failed ');6}Else if($pid= = 0){7 $mypid=Getmygid();8 Echo' I am Child process. My PID is '.$mypid. ' And my father is ',$ppid.Php_eol;9 Exit();//Close child process, need to cooperate with pcntl_wait, otherwise through PS aux | grep php->[php] <defunct> Zombie ProcessTen}Else{ one Echo' Oh my god! I am a Father now! My Child is '.$pid. ' And mine is '.$ppid.Php_eol; a //pcntl_wait ($status);//recycle sub-processes to avoid zombie processes - } - //to suspend the main process the Sleep(100);
Execute command can be seen, child process becomes zombie process
Although the zombie process does not account for any memory, but it is very annoying, the yard of a pile of dead zombies all feel Strange. (don't forget that they also occupy the Pid)
Open the 12-line comment and re-execute the script test ps aux | grep PHP can find that the child process is recycled after it exits. by the way, it can be done by signal.
Pcntl_signal (SIGCHLD, sig_ign);//如果父进程不关心子进程什么时候结束,子进程结束后,内核会回收。
Reference: http://www.jb51.net/article/56301.htm
PHP multi-process Pcntl Learning (trapping)