Solved

PHP forked process not exit completely and become zombie

Posted on 2014-12-05
2
209 Views
Last Modified: 2014-12-05
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>
0
Comment
Question by:drupal_100
2 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
Comment Utility
I still stand by my recommendation in your previous post to use a web server to do all this work for you instead of trying to rebuild the wheel. This is just one of the issues you might face by trying to use pnctl_fork. The simple answer is to simply tell the system that you don't want the child process to stick around when it exits:

pcntl_signal(SIGCHLD, SIG_IGN);

The child process sticks around by default in case you want the parent process to access any meta-information (e.g. status info) about the child after it exits.
0
 

Author Closing Comment

by:drupal_100
Comment Utility
It works. Thanks.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now