array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.pcntl-fork.php', 1 => 'pcntl_fork', ), 'up' => array ( 0 => 'ref.pcntl.php', 1 => 'PCNTL 函数', ), 'prev' => array ( 0 => 'function.pcntl-exec.php', 1 => 'pcntl_exec', ), 'next' => array ( 0 => 'function.pcntl-get-last-error.php', 1 => 'pcntl_get_last_error', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/pcntl/functions/pcntl-fork.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)
pcntl_fork — 在当前进程当前位置产生分叉(fork)
pcntl_fork() 函数创建子进程,这个子进程仅 PID(进程号) 和 PPID(父进程号)与其父进程不同。有关分叉在系统上工作的具体细节,请参阅系统的 fork(2) 手册页。
此函数没有参数。
成功时,在父进程执行线程内返回产生的子进程的 PID,在子进程执行线程内返回 0。失败时,在 父进程上下文返回 -1,不会创建子进程,并且会引发 PHP 错误。
示例 #1 pcntl_fork() 示例
<?php
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// 父进程
pcntl_wait($status); // 防止僵尸子进程
} else {
// 子进程
}
?>