Solved

PHP forked process not exit completely and become zombie

Posted on 2014-12-05
2
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

726 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