Reply to A problem with pcntl_fork

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация