Link to home
Start Free TrialLog in
Avatar of drupal_100
drupal_100Flag for United States of America

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>
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of drupal_100

ASKER

It works. Thanks.