Source of: /manual/en/function.pcntl-fork.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.pcntl.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'function.pcntl-fork.php',
1 => 'pcntl_fork',
),
'up' =>
array (
0 => 'ref.pcntl.php',
1 => 'PCNTL Functions',
),
'prev' =>
array (
0 => 'function.pcntl-exec.php',
1 => 'pcntl_exec',
),
'next' =>
array (
0 => 'function.pcntl-getpriority.php',
1 => 'pcntl_getpriority',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="function.pcntl-fork" class="refentry">
<div class="refnamediv">
<h1 class="refname">pcntl_fork</h1>
<p class="verinfo">(PHP 4 >= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">pcntl_fork</span> — <span class="dc-title">Forks the currently running process</span></p>
</div>
<a name="function.pcntl-fork.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type">int</span> <span class="methodname"><b>pcntl_fork</b></span>
( <span class="methodparam">void</span>
)</div>
<p class="para rdfs-comment">
The <b>pcntl_fork()</b> function creates a child
process that differs from the parent process only in its PID and
PPID. Please see your system's fork(2) man page for specific details as to
how fork works on your system.
</p>
</div>
<a name="function.pcntl-fork.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
On success, the PID of the child process is returned in the
parent's thread of execution, and a 0 is returned in the child's
thread of execution. On failure, a -1 will be returned in the
parent's context, no child process will be created, and a PHP
error is raised.
</p>
</div>
<a name="function.pcntl-fork.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 <b>pcntl_fork()</b> example</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br />$pid </span><span style="color: #007700">= </span><span style="color: #0000BB">pcntl_fork</span><span style="color: #007700">();<br />if (</span><span style="color: #0000BB">$pid </span><span style="color: #007700">== -</span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> die(</span><span style="color: #DD0000">'could not fork'</span><span style="color: #007700">);<br />} else if (</span><span style="color: #0000BB">$pid</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// we are the parent<br /> </span><span style="color: #0000BB">pcntl_wait</span><span style="color: #007700">(</span><span style="color: #0000BB">$status</span><span style="color: #007700">); </span><span style="color: #FF8000">//Protect against Zombie children<br /></span><span style="color: #007700">} else {<br /> </span><span style="color: #FF8000">// we are the child<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
</div>
<a name="function.pcntl-fork.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="function.pcntl-waitpid.php" class="function" rel="rdfs-seeAlso">pcntl_waitpid()</a> - Waits on or returns the status of a forked child</li>
<li class="member"><a href="function.pcntl-signal.php" class="function" rel="rdfs-seeAlso">pcntl_signal()</a> - Installs a signal handler</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>