|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2004-03-27 16:05 UTC] bhook at coder7 dot com
[2004-03-28 02:08 UTC] magnus@php.net
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 05:00:02 2025 UTC |
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.