PHP之pcntl_fork多进程

<?php
if (! function_exists('pcntl_fork')) die('PCNTL functions not available on this PHP installation');
for ($i = 1; $i <= 5; ++$i) {
        $pid = pcntl_fork();

        if (!$pid) {
            sleep(1);
            print "In child $i\n";
            for($b=0; $b<2000; $b++) {
                 sleep(1);
                 echo 'we are the child' . "$i\n";
            }
            exit($i);
        }
}

while (pcntl_waitpid(0, $status) != -1) {
        $status = pcntl_wexitstatus($status);
        echo "Child $status completed\n";
}
/*for($i = 0; $i<3; $i++) {
echo "-----------------------$i-------------------\n";
$pid = pcntl_fork();
if ($pid == -1) {
     die('could not fork');
} else if ($pid) {
     var_dump($pid);
     // we are the parent
     for($a = 0; $a<2000; $a++) {
         sleep(1);
         echo 'we are the parent' . "$a\n";
     }
     pcntl_wait($status); //Protect against Zombie children
} else {
     var_dump($pid);
     for($b=0; $b<2000; $b++) {
        sleep(1);
        echo 'we are the child' . "$b\n";
     }
     exit(0);
     // we are the child
}
} */

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容