php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27739 pcntl_fork() children go <defunct> on completion
Submitted: 2004-03-27 15:24 UTC Modified: 2004-03-28 02:08 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: bhook at coder7 dot com Assigned:
Status: Not a bug Package: *Extensibility Functions
PHP Version: 5.0.0RC1 OS: Slackware 9.1 (Linux 2.6.4)
Private report: No CVE-ID: None
 [2004-03-27 15:24 UTC] bhook at coder7 dot com
Description:
------------
When using pcntl_fork(), if a child script finishes execution (or exit/dies), the process goes defunct. I've found that with my particular needs I can reverse the normal programming method used with a server-like application, and the defunct problem goes away.

For example, if I had a script that waited for a socket connection, then I would spawn child processes to handle any connections. This causes defunct processes when the children exit. Instead, I have to spawn a child that continues listening, and the parent process handles the current connection. When the parent finishes execution, it exits cleanly, and the child becomes the new "parent".

Reproduce code:
---------------
for($a=0;$a<=10;$a++)
{
  $pid=pcntl_fork();
  if($pid==-1){die('fork error');}
  elseif($pid==0) //child process
  {
    echo "hello world\n";
    exit;
  }
  else //parent process
  {
    /* Continue Looping */
  }
}
passthru('ps -a | grep php');


Expected result:
----------------
Expected output of passthru() would be to have 1 properly functioning process listed.

Actual result:
--------------
Defunct processes are created. These processes will persist until the parent process dies.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-03-27 16:05 UTC] bhook at coder7 dot com
The child processes will go defunct if the parent doesn't retrieve their status. Adding:

pcntl_waitpid(-1,$c_status,WNOHANG);

as the last line inside the loop fixes the defunct processes.
 [2004-03-28 02:08 UTC] magnus@php.net
Only real bugs that have been fixed should have status Closed.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Jan 18 20:01:23 2020 UTC