Created
April 14, 2013 20:41
-
-
Save nb/5384126 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$pids = array(); | |
for($i=0; $i<10; ++$i) { | |
$pid = pcntl_fork(); | |
if ($pid) { | |
$pids[] = $pid; | |
} else { | |
sleep(2); | |
echo "Child " . posix_getpid() . " slept enough.\n"; | |
exit; | |
} | |
} | |
foreach( $pids as $pid ) { | |
pcntl_waitpid( $pid, $status ); | |
} | |
/* | |
→ time php fork.php | |
Child 6541 slept enough. | |
Child 6540 slept enough. | |
Child 6543 slept enough. | |
Child 6538 slept enough. | |
Child 6542 slept enough. | |
Child 6544 slept enough. | |
Child 6545 slept enough. | |
Child 6546 slept enough. | |
Child 6539 slept enough. | |
Child 6547 slept enough. | |
php fork.php 0.12s user 0.05s system 8% cpu 2.087 total | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment