Home > php教程 > PHP开发 > body text

Usage of php pcntl_fork and pcntl_fork

高洛峰
Release: 2016-12-22 16:14:45
Original
1645 people have browsed it

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 
}
Copy after login

For more articles related to the usage of php pcntl_fork and pcntl_fork, please pay attention to the PHP Chinese website!

Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!