代码之家  ›  专栏  ›  技术社区  ›  Blaine Lafreniere

为什么pcntl\u fork返回的进程ID与实际运行的进程ID不同?

php
  •  0
  • Blaine Lafreniere  · 技术社区  · 7 年前

    我在用 pcntl_fork()

    $pid = pcntl_fork();
    
    if ($pid == -1) {
        die('could not fork');
    } else if ($pid) {
        // we are the parent
        file_put_contents(dirname(__FILE__) . "/update.pid", $pid);
        //pcntl_wait($status); //Protect against Zombie children
    } else {
        $command = "php " . dirname(__FILE__) . "/my_script.php &";
        $output = shell_exec($command);
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Barmar    7 年前

    原因是 shell_exec() 运行shell进程,然后运行 php 在它自己的孩子身上。因此,您有以下流程层次结构:

    - php -- original script
        - shell -- created by pcntl_fork() and shell_exec
            - php -- created by executing "php" in shell
    

    pcntl_fork() 是第二道工序。

    菲律宾比索 通过使用 exec 命令:

    $command = "exec php " . dirname(__FILE__) . "/my_script.php";
    

    但是,如果您想这样做,就不能使用 & 在命令的末尾。为了在后台运行命令,它必须在新进程中运行,这样原始shell就可以继续运行,而不必等待它完成。

    推荐文章