当前位置:网站首页>php pcntl_ Fork create multiple child process resolution

php pcntl_ Fork create multiple child process resolution

2022-06-11 20:37:00 The world is white_ I am the only one

pcntl_fork() The function is php-pcntl The function used to create the process in the module .( I won't support it windows)

pcntl_fork —  Branch at the current position of the current process ( Subprocesses ). Translation notes :fork It's creating a subprocess , Parent and child processes From the fork Start from the position of... And continue down , The difference is that during the execution of the parent process , Got fork The return value is the child process Number , And what the subprocess gets is 0.

It is worth noting that , Not at all pcntl_fork When the subprocess is called, it will return 0, It's when the program runs to pcntl_fork Will generate a new branch , The main process continues to execute the code of the main process , Will also receive pcntl_fork The return value of the new branch is pid, At the same time, the new branch will continue to execute the same code from the location of the current branch , The difference is that the return value he gets is 0, This can be used to determine whether it is a new branch .

Official documents :PHP: pcntl_fork - Manual

Official website example :

<?php

$pid = pcntl_fork();
// Both the parent and child processes execute the following code 
if ($pid == -1) {
    // Error handling : Returns... When the creation of a child process fails -1.
     die('could not fork');
} else if ($pid) {
     // The parent process will get the child process number , So here is the logic of the parent process execution 
     pcntl_wait($status); // Wait for the subprocess to break , Prevent child processes from becoming zombie processes .
} else {
     // Subprocesses get $pid by 0,  So here's the logic of subprocess execution .
}

?>

fork Operation logic demonstration :

function text()
{
    for ($i = 0; $i < 3; $i++) {

        $pid = pcntl_fork();
        echo "\$i($i)" . ' >>  The main process :' . posix_getpid() . " >> fork Create child process :" . $pid . PHP_EOL;
    }
}

  Mind mapping

You can see clearly from the above two figures pcntl_fork The process of ,  that : You can see the above for loop , How many subprocesses will actually be generated ? The answer is 7 individual , altogether 8 A process (1 Parent process ,7 Subprocess ), Like the nested dreams in the movie inception ? You can find that sub processes can also be carried out in time  pcntl_fork operation , Therefore, in order to make the program run normally, the following writing method will be used to complete the multi process operation , When the logic is executed , Need to use die/return/exit To end the child process and continue nesting

function text1()
{
    $child_process =[];
    for ($i = 0; $i < 3; $i++) {
        $pid = pcntl_fork();
        // Both the parent and child processes execute the following code 
        if ($pid == -1) {
            // Error handling : Returns... When the creation of a child process fails -1.
            die('could not fork');
        } else if ($pid) {
            // The parent process will get the child process number , So here is the logic of the parent process execution 
            $child_process[] = $pid;
        } else {
            // Subprocesses get $pid by 0,  So here's the logic of subprocess execution .
            echo '1'.PHP_EOL;
            return; // By using die | return | exit  To restrict program execution 
        }
    }

    while (count($child_process) > 0) {
        foreach ($child_process as $key => $pid) {
            $res = pcntl_waitpid($pid, $status, WNOHANG);

            //-1 representative error,  Greater than 0 Represents that the child process has exited , Returned by subprocess pid, When not blocked 0 Represents that the exit subprocess is not retrieved 
            if ($res == -1 || $res > 0)
                unset($child_process[$key]);
        }
        sleep(1);
    }
}

function main()
{
    $i = 0;
    while ($i < 3) {
        $pid = pcntl_fork();
        $i++;
        if ($pid == 0) {
            //echo " Subprocesses ".PHP_EOL;
            echo time() . "-" . PHP_EOL;
            sleep(10);
            echo 1;
            return;
        }
    }
}

  Finally, share some good articles :

【 Process management 】fork What exactly does the child process copy from the parent process ?_ The blog of the running tortoise -CSDN Blog _fork Copy the process

原网站

版权声明
本文为[The world is white_ I am the only one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112034181453.html