Created
July 19, 2017 06:36
-
-
Save sng2c/853146b22929f0610cf0ef19018975c9 to your computer and use it in GitHub Desktop.
This file contains 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 = []; | |
for($i=0; $i<10; $i++){ | |
$pid = pcntl_fork(); | |
if( $pid == -1 ){ | |
die('could not fork'); | |
} | |
if( $pid ){ # 부모 : 자식 PID 누적 | |
$pids[] = $pid; | |
} | |
else{ # 자식 : 작업하고 바로 종료. | |
$sleep = rand(1,5); | |
$pid = getmypid(); | |
echo "CHILD #$pid - Sleep for $sleep seconds\n"; | |
sleep($sleep); | |
echo "CHILD #$pid - Bye!!\n"; | |
exit; | |
} | |
} | |
foreach($pids as $pid){ | |
print "PARENT - waiting #$pid\n"; | |
pcntl_waitpid($pid, $status, WUNTRACED); | |
} | |
print "done\n"; | |
Author
sng2c
commented
Jul 19, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment