Solved

PHP forked process not exit completely and become zombie

Posted on 2014-12-05
2
212 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

861 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

25 Experts available now in Live!

Get 1:1 Help Now