Home > php教程 > php手册 > body text

php pcntl_fork和pcntl_fork 的用法

WBOY
Release: 2016-06-13 12:24:11
Original
929 people have browsed it

pcntl_fork()函数就是为当前的进程创建一个子进程。并且先运行父进程,返回的是子进程的PID,肯定大于零。在父进程的代码中可以用pcntl_fork(&$status)暂停父进程知道他的子进程有返回值。注意:父进程的阻塞同时会阻塞子进程。但是父进程的结束不影响子进程的运行。
父进程运行完了会接着运行子进程,这时子进程会从执行pcntl_fork()的那条语句开始执行(包括此函数),但是此时它返回的是零(代表这是一个子进程)。在子进程的代码块中最好有exit语句,即执行完子进程后立即就结束。否则它会又重头开始执行这个脚本的某些部分(一直没有总结出规矩)。
总之,注意两点:
1。子进程最好有一个exit;语句,防止不必要的出错;
2.

复制代码 代码如下:


$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
}

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!