다음 PHP 스크립트가 있습니다. 5 가지 다른 자식 프로세스 (pcntl_fork() 함수 사용)를 사용하여 5 가지 다른 작업을 수행해야합니다. 차일 즈는 올바르게 만들어지며 물건을 만들지 만 절대 나가지 않은 것처럼 보입니다. 부모는 끝내기를 기다리지 만 결코 일어나지 않습니다. 아무도 내가 뭘 잘못하고 있는지 말해 줄 수 있니?PHP - 하위 프로세스가 제대로 종료되지 않습니다.
<?php
for ($i=1; $i<=5; $i++) {
$pid = pcntl_fork();
if ($pid == -1) {
die("Error Creating subprocess ".$i);
exit(-1);
}
else if (!$pid) {
switch ($i) {
case 1:
// Child 1
error_log("I\'m the child number ".$i." PID: ".getmypid()."\n", 3, "/var/log/php_errors.log");
//Doing some stuff
exit;
case 2:
// Child 2
error_log("I\'m the child number ".$i." PID: ".getmypid()."\n", 3, "/var/log/php_errors.log");
//Doing some stuff
exit;
case 3:
// Child 3
error_log("I\'m the child number ".$i." PID: ".getmypid()."\n", 3, "/var/log/php_errors.log");
//Doing some stuff
exit;
case 4:
// Child 4
error_log("I\'m the child number ".$i." PID: ".getmypid()."\n", 3, "/var/log/php_errors.log");
//Doing some stuff
exit;
case 5:
// Child 5
error_log("I\'m the child number ".$i." PID: ".getmypid()."\n", 3, "/var/log/php_errors.log");
//Doing some stuff
exit;
default:
break;
}
}
else {
$pids[] = $pid;
}
}
error_log("Luke, I\'m your father. PID: ".getmypid()."\n", 3, "/var/log/php_errors.log");
foreach ($pids as $key => $child) {
$res = pcntl_waitpid($child, $status);
//Check if this child has exited normally
if (pcntl_wifexited($status))
error_log("Child ".$child." has ended\n", 3, "/var/log/php_errors.log");
else
error_log("Child ".$child." is zombie\n", 3, "/var/log/php_errors.log");
if($res == -1 || $res > 0)
unset($pids[$key]);
}
?>
스위치에는 항상 기본 사례가 있어야합니다. (이것은 단지 참고 사항입니다) –
고마워요! 방금 게시물을 편집했습니다. – serenhopy
왜 종료합니까? –