多进程编写需要清楚的几个问题

  1. 创建了几个进程?

  2. 每个进程 $count 是多少?

  3. 每个进程从哪个地方开始运行代码的?

  4. fork 之后,每个进程的变量 $i, $count 的值是多少 ?

  5. 每个进程运行到哪一行语句结束?

示例一:

代码

<?php

$count = 10;

for ($i = 0; $i < 2; $i++){

    $pid = pcntl_fork();
    // step1-1  parent           创建子进程 child-1: count = 10, i = 0
    // step3-1  cpu 调度 parent  创建子进程 child-2: count = 100, i = 1
    // step5-1  cpu 调度 child-1 创建子进程 child-3: count = 11,  i = 1

    if ($pid == 0){

        $count += 1;
        // step1-2 child-1 执行:count = 11,   i = 1
        // step4   child-2 执行: count = 101,  i = 2
        //         child-2 最终结果:count = 101
        // step6   child-3 执行:count = 12,   i = 2 

    } else {

        $count *= 10;
        // step2   cpu 调度 parent:count = 100,   i = 1
        // step3-2 cpu 调度 parent:count = 1000,  i = 2     for循环退出
        //         parent 最终结果:count = 1000
        // step5-2 cpu 调度 child-1:count = 110,    i = 2
        //         child-1 最终结果:count = 110

    }

}

while(1){

    fprintf(STDOUT, "pid=%d,count=%d\n", posix_getpid(), $count);
    sleep(3);

}

分析:

  • step1-1 parent 创建子进程 child-1:count = 10, i = 0

  • step1-2 child-1 执行:count = 11, i = 1

  • step2 cpu 调度 parent:count = 100, i = 1

  • step3-1 cpu 调度 parent 创建子进程 child-2: count = 100, i = 1

  • step3-2 cpu 调度 parent:count = 1000, i = 2 for循环退出

    parent 最终结果:count=1000

  • step4 child-2 执行: count = 101, i = 2

    child-2 最终结果:count = 101

  • step5-1 cpu 调度 child-1 创建子进程 child-3: count = 11, i = 1

  • step5-2 cpu 调度 child-1:count = 110, i = 2

    child-1 最终结果:count = 110

  • step6 child-3 执行:count = 12, i = 2

运行结果:


pid=1801,count=1000
pid=1803,count=101
pid=1802,count=110
pid=1804,count=12

...

进程间关系:

    ├─bash,80
    │   └─php,1801 demo9.php
    │       ├─php,1802 demo9.php
    │       │   └─php,1804 demo9.php
    │       └─php,1803 demo9.php

示例二

代码

<?php

$count = 10;

for ($i = 0; $i < 2; $i++){

    $pid = pcntl_fork();
    // step1-1  parent           创建子进程 child-1: count = 10, i = 0
    // step3-1 cpu 调度 child-1  创建子进程 child-2: count = 11,   i = 1

    if ($pid == 0){

        $count += 1;
        // step1-2 child-1 执行:count = 11,   i = 1
        // step4   cpu 调度 child-2 创建子进程 child-2: count = 12,   i = 2

    } else {

        $count *= 10;
        break;
        // step2   cpu 调度 parent:count = 100, i = 0  break 跳出循环
        //         parent 最终结果:count = 100

        // step3-2 cpu 调度 child-1:count = 110, i = 2  break 跳出循环
        //         parent 最终结果:count = 110

    }

}

while(1){

    fprintf(STDOUT, "pid=%d,count=%d\n", posix_getpid(), $count);
    sleep(3);

}

分析

  • step1-1 parent 创建子进程 child-1: count = 10, i = 0

  • step1-2 child-1 执行:count = 11, i = 1

  • step2 cpu 调度 parent:count = 100, i = 0 break 跳出循环

    parent 最终结果:count = 100

  • step3-1 cpu 调度 child-1 创建子进程 child-2: count = 11, i = 1

  • step3-2 cpu 调度 child-1:count = 110, i = 2 break 跳出循环

    parent 最终结果:count = 110

  • step4 cpu 调度 child-2 创建子进程 child-2: count = 12, i = 2

运行结果

pid=1962,count=100
pid=1963,count=110
pid=1964,count=12

进程间关系

  ├─bash,80
  │   └─php,1962 demo9.php
  │       └─php,1963 demo9.php
  │           └─php,1964 demo9.php