[php-src] Issue #14117: pcntl_fork children are utilizing just a single core on FreeBSD 13.3+
| From: | instaproofs | Date: | Thu, 02 May 2024 23:05:10 +0000 |
| Subject: | [php-src] Issue #14117: pcntl_fork children are utilizing just a single core on FreeBSD 13.3+ | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246850@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/14117
Author: instaproofs
### Description
Using PHP 8.2.18, I have a script that processes a series of images, performing various conversions
and resizing tasks. To make the most of the server's multiple cores, I use the pcntl_fork()
function to create child processes that can simultaneously handle different images. This means
instead of processing images sequentially, each image can be processed concurrently on separate
cores.
For instance, if I have 10 images to process and each takes 3 seconds individually, without parallel
processing, it would take a total of 30 seconds. However, with parallel processing, all 10 images
can finish processing simultaneously in just 3 seconds.
This approach has been effective until we updated to FreeBSD 13.3. After the update, the forked
processes no longer distribute across different cores; instead, they all run on a single core.
Consequently, if I have 10 forked processes running, each is constrained to using only 10% of a
single core, resulting in a 10-fold increase in processing time.
We've conducted tests with FreeBSD versions ranging from 9.x up to 13.2-RELEASE-p11 and found
that the issue doesn't occur. However, when we update to FreeBSD 13.3, the problem occurs every
time.
We've tested across several different PHP versions, from 8.1x up to 8.3x, and they all yield
the same results.
This may sound like a FreeBSD issue, but I am at a loss and exploring all avenue for it.
Anyone have any ideas on what might be causing this issue, or any suggestions for potentially
resolving it?
Sample code for forking the processes...
```php
<?PHP
[...]
$forkedProcessIds = array();
for($i =0; $i < $imagesToProcessCount; $i++) {
$pid = pcntl_fork();// Create a child process
if($pid == -1) {
exit(-1); // Could not fork the process
} else if($pid) {
$forkedProcessIds[] = $pid; // Track of the child IDs
} else {
// Do image processing stuff
[...]
}
}
// Wait for the forked processes to finish
while ($childPID = pcntl_waitpid(0, $status)) {
if ($childPID == -1) {
// No child processes left to wait for
break;
}
// Find out which child process exited
$exitPID = pcntl_wexitstatus($status);
// Handle the exit status based on the child process PID
if (in_array($exitPID, $forkedProcessIds)) {
// Remove the child process ID from the tracking array
$index = array_search($exitPID, $forkedProcessIds);
if ($index !== false) {
unset($forkedProcessIds[$index]);
}
}
}
?>
```
### PHP Version
PHP 8.2.18
### Operating System
FreeBSD 13.3