|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2020-06-08 20:11 UTC] alexinbeijing at gmail dot com
[2020-06-08 22:12 UTC] phpbugs at muglug dot com
-Status: Open
+Status: Closed
[2020-06-08 22:12 UTC] phpbugs at muglug dot com
[2020-06-09 10:49 UTC] cmb@php.net
-Status: Closed
+Status: Not a bug
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 14 18:00:02 2025 UTC |
Description: ------------ I have a cross-platform command-line application that uses pcntl_fork to spawn child processes. pcntl_fork's implementation is very simple – it's just a call to fork(), which has copy-on-write behaviour (no memory should be duplicated unless the program modifies it). On Macs, memory appears to be shared effectively – it's only dirtied if there is some change made to the data. When the same script is run on Linux it appears that the shared memory is dirtied instantly, which increases the cost of pcntl_fork both in terms of memory (whicch is multiplied by the number of calls to that function) and also time, as each call to pcntl_fork is more costly. Test script: --------------- <?php $a = []; for ($i = 0; $i < 1000000; $i++) { $a[] = "$i"; } $pid = pcntl_fork(); if ($pid == -1) { die('could not fork'); } else if ($pid) { // we are the parent pcntl_wait($status); //Protect against Zombie children } else { echo shell_exec('cat /proc/' . posix_getpid() . '/smaps | grep Shared_ | grep -v "0 kB"'); echo $a[0]; } Expected result: ---------------- There should be no large areas of Shared_Dirty memory in the output, since most memory should be shared.