Skip to content

Instantly share code, notes, and snippets.

@sng2c
Created July 19, 2017 06:36
Show Gist options
  • Save sng2c/853146b22929f0610cf0ef19018975c9 to your computer and use it in GitHub Desktop.
Save sng2c/853146b22929f0610cf0ef19018975c9 to your computer and use it in GitHub Desktop.
<?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";
@sng2c
Copy link
Author

sng2c commented Jul 19, 2017

CHILD #69644 - Sleep for 2 seconds
CHILD #69645 - Sleep for 3 seconds
CHILD #69646 - Sleep for 4 seconds
CHILD #69647 - Sleep for 1 seconds
CHILD #69648 - Sleep for 1 seconds
CHILD #69649 - Sleep for 3 seconds
CHILD #69650 - Sleep for 4 seconds
CHILD #69651 - Sleep for 5 seconds
CHILD #69652 - Sleep for 5 seconds
PARENT - waiting# 69644
CHILD #69653 - Sleep for 5 seconds
CHILD #69647 - Bye!!
CHILD #69648 - Bye!!
CHILD #69644 - Bye!!
PARENT - waiting# 69645
CHILD #69645 - Bye!!
CHILD #69649 - Bye!!
PARENT - waiting# 69646
CHILD #69646 - Bye!!
CHILD #69650 - Bye!!
PARENT - waiting# 69647
PARENT - waiting# 69648
PARENT - waiting# 69649
PARENT - waiting# 69650
PARENT - waiting# 69651
CHILD #69652 - Bye!!
CHILD #69651 - Bye!!
CHILD #69653 - Bye!!
PARENT - waiting# 69652
PARENT - waiting# 69653
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment