Home  >  Article  >  Backend Development  >  p c php pcntl_fork and pcntl_fork usage

p c php pcntl_fork and pcntl_fork usage

WBOY
WBOYOriginal
2016-07-29 08:39:40926browse

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 over and execute certain parts of the script (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 the code The code is as follows:


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

The above introduces the usage of p c php pcntl_fork and pcntl_fork, including p c content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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