note 41150 deleted from function.pcntl-fork by danbrown
| From: | danbrown@php.net | Date: | Tue, 10 Aug 2010 15:49:02 +0000 |
| Subject: | note 41150 deleted from function.pcntl-fork by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-171545@lists.php.net to get a copy of this message | ||
Note Submitter: astrolox at lawyersonline dot co dot uk
----
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