Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions reference/pcntl/functions/pcntl-wait.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>resource_usage</parameter></term>
<listitem>
<para>
Stores the resource usage information for the child that exited as an
associative <type>array</type>. For information on the contents see
<function>getrusage</function>.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
Expand All @@ -99,12 +109,71 @@
</para>
</refsect1>

<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>pcntl_wait</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
function checkChildren(bool $wait): void
{
$flags = ($wait ? 0 : WNOHANG);
$deadPid = pcntl_wait($status, $flags, $rusage);
if ($deadPid === -1) {
$errNo = pcntl_get_last_error();
$errMsg = pcntl_strerror($errNo);
print "PARENT pcntl_wait FAILED ({$deadPid}): {$errMsg}\n";
} elseif ($deadPid === 0) {
print "PARENT pcntl_wait: No exited children.\n";
} else {
print "PARENT pcntl_wait: Child PID {$deadPid} exited with status: "
. pcntl_wexitstatus($status) ."\n";
var_dump($rusage);
}
}
$parentPid = getmypid();
$forkedPid = pcntl_fork();
if ($forkedPid == -1) {
die('Could not fork');
} else if ($forkedPid) {
// Parent process
print "PARENT {$parentPid} has forked {$forkedPid}\n";
print "PARENT is checking for exited children\n";
checkChildren(false);
// Prevents children remaining as a "zombie process"
print "PARENT is waiting for a child to exit\n";
checkChildren(true);
print "PARENT is checking for exited children\n";
checkChildren(false);
print "PARENT is exiting\n";
} else {
// Child process
$pid = getmypid();
print "CHILD {$pid} is sleeping\n";
sleep(10);
print "CHILD is exiting\n";
exit(7);
}
]]>
</programlisting>
</example>
</para>
</refsect1>

<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>pcntl_fork</function></member>
<member><function>pcntl_signal</function></member>
<member><function>pcntl_waitpid</function></member>
<member><function>pcntl_wifexited</function></member>
<member><function>pcntl_wifstopped</function></member>
<member><function>pcntl_wifsignaled</function></member>
Expand Down
69 changes: 69 additions & 0 deletions reference/pcntl/functions/pcntl-waitpid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>resource_usage</parameter></term>
<listitem>
<para>
Stores the resource usage information for the child that exited as an
associative <type>array</type>. For information on the contents see
<function>getrusage</function>.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
Expand All @@ -143,12 +153,71 @@
</para>
</refsect1>

<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
Comment on lines +161 to +163
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless wrapping para tag

<title><function>pcntl_waitpid</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
function checkChild(int $pid, bool $wait): void
{
$flags = ($wait ? 0 : WNOHANG);
$deadPid = pcntl_waitpid($pid, $status, $flags, $rusage);
if ($deadPid === -1) {
$errNo = pcntl_get_last_error();
$errMsg = pcntl_strerror($errNo);
print "PARENT pcntl_waitpid FAILED ({$deadPid}): {$errMsg}\n";
} elseif ($deadPid === 0) {
print "PARENT pcntl_waitpid: No exited children.\n";
} else {
print "PARENT pcntl_waitpid: Child PID {$deadPid} exited with status: "
. pcntl_wexitstatus($status) ."\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation for wexitstatus says:

This function is only useful if pcntl_wifexited() returned true.

So, is a wifexited call missing here? Looking more carefully at the example, it uses quite a few functions from the pcntl extension. It might make sense to include it in https://www.php.net/manual/en/pcntl.example.php instead.

var_dump($rusage);
}
}

$parentPid = getmypid();
$forkedPid = pcntl_fork();
if ($forkedPid == -1) {
die('Could not fork');
} else if ($forkedPid) {
// Parent process
print "PARENT {$parentPid} has forked {$forkedPid}\n";

print "PARENT is checking for exited children\n";
checkChild($forkedPid, false);

// Prevents children remaining as a "zombie process"
print "PARENT is waiting for a child to exit\n";
checkChild($forkedPid, true);

print "PARENT is checking for exited children\n";
checkChild($forkedPid, false);

print "PARENT is exiting\n";
} else {
// Child process
$pid = getmypid();
print "CHILD {$pid} is sleeping\n";
sleep(10);
print "CHILD is exiting\n";
exit(7);
}
]]>
</programlisting>
</example>
</para>
</refsect1>

<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>pcntl_fork</function></member>
<member><function>pcntl_signal</function></member>
<member><function>pcntl_wait</function></member>
<member><function>pcntl_wifexited</function></member>
<member><function>pcntl_wifstopped</function></member>
<member><function>pcntl_wifsignaled</function></member>
Expand Down