Home > Backend Development > PHP Tutorial > Usage of php pcntl_fork and pcntl_fork_PHP tutorial

Usage of php pcntl_fork and pcntl_fork_PHP tutorial

WBOY
Release: 2016-07-21 15:47:04
Original
794 people have browsed it

The 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;
2.

Copy code The code is as follows:

$pid = pcntl_fork();
//It is best not to have other statements here
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
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320018.htmlTechArticleThe 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. You can use pcn in the code of the parent process...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template