pcntl_fork() function creates a child process for the current process. And run the parent process first, and return the PID of the child process, which must be greater than zero. In the code of the parent process, you can use pcntl_fork (&$status) to pause the parent process until its child process has a return value. Note: Blocking of the parent process will also block the child process. However, the end of the parent process does not affect the operation of the child process.
After the parent process has finished running, it will then run the child process. At this time, the child process will start executing from the statement that executes pcntl_fork() (including this function), but at this time it returns zero (meaning that this is a child process). It is best to have an exit statement in the code block of the child process, that is, it will end immediately after executing the child process. Otherwise it will start executing some parts of the script again (without ever figuring out the rules).
In short, pay attention to two points:
1. It is best for the child process to have an exit; statement to prevent unnecessary errors;
$pid = pcntl_fork(); //这里最好不要有其他的语句 if ($pid == -1) { die('could not fork'); } else if ($pid) { // we are the parent pcntl_wait($status); //Protect against Zombie children } else { // we are the child }
For more articles related to the usage of php pcntl_fork and pcntl_fork, please pay attention to the PHP Chinese website!