3

我在 PHPUnit 中使用 pcntl_fork() 有一些问题。我正在执行这段代码

class ForkTest extends PHPUnit_Framework_TestCase {
    public function test1() {
        print('Start test with pid '.posix_getpid()."\n");

        $pids = [];

        for ($count = 0; $count < 3; ++$count) {
            $pids[$count] = pcntl_fork();
            if (-1 == $pids[$count]) {
                die("It's a trap, iteration $count\n");
            } else if (!$pids[$count]) {
                print("I'm child with pid ".posix_getpid()."\n");
                exit();
            }
            print('Parent: after fork, new child: '.$pids[$count]."\n");
        }

        for ($count = 0; $count < 3; ++$count) {
            $status = 0;
            if ($pids[$count] != pcntl_waitpid($pids[$count], $status)) {
                die("Error with wait pid $count.\n");
            } else {
                print('Process with pid '.$pids[$count]." finished\n");
            }
        }
    }
}

我得到以下信息:

Start test with pid 15886
Parent: after fork, new child: 15887
Parent: after fork, new child: 15888
I'm child with pid 15889
Start test with pid 15886
Parent: after fork, new child: 15887
I'm child with pid 15888
Start test with pid 15886
I'm child with pid 15887
.Start test with pid 15886
Parent: after fork, new child: 15887
Parent: after fork, new child: 15888
Parent: after fork, new child: 15889
Process with pid 15887 finished
Process with pid 15888 finished
Process with pid 15889 finished

但我期待这样的事情(我在没有使用 PHPUnit 的情况下运行测试时得到了它):

Start test with pid 15907
Parent: after fork, new child: 15908
Parent: after fork, new child: 15909
Parent: after fork, new child: 15910
I'm child with pid 15910
I'm child with pid 15909
I'm child with pid 15908
Process with pid 15908 finished
Process with pid 15909 finished
Process with pid 15910 finished

那么,这是 PHPUnit 的预期行为吗?有什么办法可以解决吗?

4

0 回答 0