Php pcntl_fork child process exit exit problem

the parent process uses while (1) to suspend the process, and exit (1) is used in the child process, but in the end, ps-ef | grep php found that the child process did not exit. What caused the problem? normally, the child process should have exited?

<?php
for($i=0;$i<10;$iPP){
    fork_worker();
}


function fork_worker(){
    $pid = pcntl_fork();
    if($pid == 0){ //child processes
        echo "\r\n";
        $this_id = getmypid();
        echo  $this_id."\r\n";
        exit(1);
    }elseif($pid > 0){ //master processes
        echo "\r\n";
    }
}
while(1);
?>

forget to answer questions

Php
Aug.27,2021

PHP the child process that comes out of the main process fork needs to wait for the child process to exit through the pcntl_wait method. The exit method provided by PHP exits the execution of the script, not the process. For instructions on PHP processes, please refer to http://php.net/manual/zh/ref.


if the parent process does not call wait , the child process will become a zombie process and will not be recycled.

<?php
$id = [];
for($i=0;$i<10;$iPP){
    $id[] = fork_worker();
}

function fork_worker(){
    $pid = pcntl_fork();
    if($pid == 0){ //child processes
        echo "\r\n";
        $this_id = getmypid();
        echo  $this_id."\r\n";
        exit(1);
    }elseif($pid > 0){ //master processes
        echo "\r\n";
        return $pid;
    }
}

foreach($id as $i) {
    pcntl_wait($i);
}
while(1);
?>

exit is the exit of the execution process. However, when a child process exits, resources need to be reclaimed by the working parent process. For example, if you call pcntl_wait, it will become a zombie process if it is not recycled, which will still take up a small amount of system resources. If the parent process exits, the child process will be taken over and recycled by the kernel init process.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e725d0-5b1e9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e725d0-5b1e9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?