PHP multi-process Pcntl Learning (trapping)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.