对于用PHP进行多进程并发编程,不可避免要遇到僵尸进程的问题。
$pid = pcntl_fork(); if($pid == -1) { die('fork error'); } else if ($pid) { //父进程阻塞着等待子进程的退出 //pcntl_wait($status); //pcntl_waitpid($pid, $status); //非阻塞方式 //pcntl_wait($status, WNOHANG); //pcntl_waitpid($pid, $status, WNOHANG); } else { sleep(3); echo "child \r\n"; exit; }
<?php declare(ticks = 1); //信号处理函数 function sig_func() { echo "SIGCHLD \r\n"; pcntl_wait($status); //pcntl_waitpid(-1, $status); //非阻塞 //pcntl_wait($status, WNOHANG); //pcntl_waitpid(-1, $status, WNOHANG); } pcntl_signal(SIGCHLD, 'sig_func'); $pid = pcntl_fork(); if($pid == -1) { die('fork error'); } else if ($pid) { sleep(10); } else { sleep(3); echo "child \r\n"; exit; }
<?php $pid = pcntl_fork(); if($pid == -1) { die('fork error'); } else if ($pid) { for(;;) { sleep(3); } } else { echo "child \r\n"; exit; }
> ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]'
<?php declare(ticks = 1); //信号处理函数 function sig_func() { echo "SIGCHLD \r\n"; pcntl_waitpid(-1, $status, WNOHANG); } pcntl_signal(SIGCHLD, 'sig_func'); $pid = pcntl_fork(); if($pid == -1) { die('fork error'); } else if ($pid) { for(;;) { sleep(3); } } else { echo "child \r\n"; exit; }
<?php declare(ticks = 1); pcntl_signal(SIGCHLD, SIG_IGN); $pid = pcntl_fork(); if($pid == -1) { die('fork error'); } else if ($pid) { for(;;) { sleep(3); } } else { echo "child \r\n"; exit; }
<?php $pid = pcntl_fork(); if($pid == -1) { die('fork error'); } else if ($pid) { //父进程等待子进程退出 pcntl_wait($status); echo "parent \r\n"; } else { //子进程再fork一次,产生孙进程 $cpid = pcntl_fork(); if($cpid == -1) { die('fork error'); } else if ($cpid) { //这里是子进程,直接退出 echo "child \r\n"; exit; } else { //这里是孙进程,处理业务逻辑 for($i = 0; $i < 10; ++$i) { echo "work... \r\n"; sleep(3); } } }