drupal_100
asked on
PHP forked process not exit completely and become zombie
PHP forked process not exit completely and become zombie
This is a simplified program.
The test.php is a daemon program, always running there.
It forks process to work on some task.
Once finishing work, the forked process exits.
But when the forked process exits, it becomes zombie.
How to make the forked process exit completely without becoming zombie?
#!/usr/bin/php
<?php
while (1) {
sleep(1);
$pid = pcntl_fork();
if (!$pid) {
$mypid = getmypid();
sleep(5);
print "pid=$mypid finish work \n";
exit();
}
sleep(1);
} // while
?>
./test.php
... daemon running ...
$ ps -ef | grep mqp
ubuntu 10084 10073 0 12:21 pts/0 00:00:00 /usr/bin/php ./test.php
ubuntu 10085 10073 0 12:21 pts/0 00:00:00 /usr/bin/php ./test.php
ubuntu 10074 10073 0 12:21 pts/0 00:00:00 [test.php] <defunct>
ubuntu 10075 10073 0 12:21 pts/0 00:00:00 [test.php] <defunct>
This is a simplified program.
The test.php is a daemon program, always running there.
It forks process to work on some task.
Once finishing work, the forked process exits.
But when the forked process exits, it becomes zombie.
How to make the forked process exit completely without becoming zombie?
#!/usr/bin/php
<?php
while (1) {
sleep(1);
$pid = pcntl_fork();
if (!$pid) {
$mypid = getmypid();
sleep(5);
print "pid=$mypid finish work \n";
exit();
}
sleep(1);
} // while
?>
./test.php
... daemon running ...
$ ps -ef | grep mqp
ubuntu 10084 10073 0 12:21 pts/0 00:00:00 /usr/bin/php ./test.php
ubuntu 10085 10073 0 12:21 pts/0 00:00:00 /usr/bin/php ./test.php
ubuntu 10074 10073 0 12:21 pts/0 00:00:00 [test.php] <defunct>
ubuntu 10075 10073 0 12:21 pts/0 00:00:00 [test.php] <defunct>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER