| 
	
 | 
 Posted by Thomas on 11/17/06 23:09 
I'm having some issues with forking and defunct children. This is my code: 
 
#!/usr/bin/php 
<?php 
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr); 
 
/* 
* LISTEN FOR CONNECTIONS 
*/ 
while(TRUE){ 
    while($conn = stream_socket_accept($socket,-1)){ 
        $pid = pcntl_fork(); 
        if($pid==-1){ 
            fwrite(STDOUT,"Fork failed. Exiting!\n"); 
            exit; 
        }elseif($pid){ 
            /* 
            * We are parent. 
            * Close connection 
            */ 
            fclose($conn); 
        }else{ 
            fwrite($conn, "Hello!\n"); 
            fwrite($conn, "I am PID: ".posix_getpid()."\n"); 
            sleep(2); 
            fclose($conn); 
            exit; 
        } 
    } 
} 
 
function sig_handler($signo){ 
    switch ($signo) { 
        case SIGTERM: 
            exit; 
            break; 
        case SIGCHLD: 
            exit; 
            break; 
        default: 
            // handle all other signals 
    } 
} 
 
// setup signal handlers 
pcntl_signal(SIGTERM, "sig_handler"); 
pcntl_signal(SIGCHLD, "sig_handler"); 
?> 
 
When the child exits (after the 2 seconds sleep), it leaves a defunct 
zombie running ([scriptname] <defunct>), and I can't get rid of it 
unless I kill the parent script. 
 
What I'm hoping to accomplish is removing the child completely on exit. 
How can I do this? 
 
I've read the manual on pcntl_fork on php.net (and all the comments), 
but nothing seems to work. I know it's me doing something completely 
wrong, I just can't figure out what. 
 
I hope someone can point me in the right direction.  :o) 
 
Regards 
Thomas
 
  
Navigation:
[Reply to this message] 
 |