Solved

PHP forked process not exit completely and become zombie

Posted on 2014-12-05
2
216 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
ID: 40483578
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
ID: 40483793
It works. Thanks.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 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