note 41150 added to function.pcntl-fork
| From: | astrolox at lawyersonline dot co dot uk | Date: | Wed, 31 Mar 2004 20:40:12 +0000 |
| Subject: | note 41150 added to function.pcntl-fork | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-67430@lists.php.net to get a copy of this message | ||
When using php 4.3.3, I found that output buffering can really confuse you if you use fork within
your script.
The buffer (everything which hasn't been placed on the screen yet) is copied to the child
process.
This means that when the child flushes it's buffer, which happens automatically when the script
ends, you will get a copy of everything which your code (before the fork) tried to place on to the
screen.
This causes it to look like all your code before the fork has run twice, when actually it
hasn't.
This doesn't happen on my other server running a newer version of php, but that might be a
configuration issue.
Note that using ob_implicit_flush() at the start of the script doesn't appear to solve the
problem. Adding ob_end_flush() before the pcntl_fork() command does solve the problem.
Example:
<?
echo "This is an echo before I called the fork command\n";
// uncomment the next line to fix the described problem
#ob_end_flush();
$pid = pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
echo "I am the parent, pid = ". $pid ."\n";
} else {
echo "I am the child, pid = ". $pid ."\n";
}
?>
Output, before "fixed":
This is an echo before I called the fork command
I am the parent, pid = 15449
This is an echo before I called the fork command
I am the child, pid = 0
Output, after "fixed":
This is an echo before I called the fork command
I am the parent, pid = 15023
I am the child, pid = 0
----
Manual Page -- http://www.php.net/manual/en/function.pcntl-fork.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41150
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41150&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41150&report=yes
Search -- http://master.php.net/manage/user-notes.php