Scott Hill wrote:
On 7/23/07, *Orson Jones* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    Looking around php.net <http://php.net> I found this

    http://us2.php.net/manual/en/function.pcntl-fork.php

    You can do a standard fork() using php. Check it out.

    Orson



If you don't care about the child process after it is finished, you must do a pcntl_signal(SIGCHLD,SIG_IGN); before the fork. This will make the parent process ignore any signal sent back by a dying child. When the child process sees that it's kill signal was either processed or ignored, it dies quietly. This prevents what is known as zombie or orphan processes in *nix, which is a dead child process that has been shunned by its parent. If you want to track the process of the child process, leave out the pcntl_signal and use pcntl_waitpid after the fork. This is a very, very common occurance in the *nix world. For example, the apache server forks a child process for every request on its socket(s). And I don't care what anyone says (about XP or Vista), Windows still sucks when it comes to forking any kind of process.

My humble $.02.
I think I've got pcntl_fork() working (see below). The tricky part is that pcntl_* functions can only be called in cgi mode (i.e. from the command line). One thing that is puzzling is that $argv[0] equals 'fork.php' in sleep.php. I haven't yet tested to see if this works on Windows. You mention Windows--are you saying that this would probably fail on Windows 2003 Server? (My target platforms are Win 2003 Server and CentOS Linux)

With this setup, it looks like I might be able to make a Fork class so that I can fork any file with something like Fork::spawn($filename);. It would require exec() access, but that is required for this particular environment anyway.

-- Ken



fork.php:
<?php
$path = '/home/ksnyder/www/test/sleep.php';
exec("php $path");
echo "<p>Called script $path and didn't wait for return value</p>";
?>

sleep.php:
<?php
$logfile = '/home/ksnyder/www/test/sleep.log';

if (!function_exists('pcntl_fork')) {
 die('Error: function pcntl_fork does not exist');
}
pcntl_signal(SIGCHLD, SIG_IGN);
$pid = pcntl_fork();
if ($pid == -1) {
 // Something went wrong (handle errors here)
 file_put_contents($logfile, "Error, unable to fork!");
} elseif ($pid == 0) {
 // This part is only executed in the child
 sleep(10);
file_put_contents($logfile, date('Y-m-d H:i:s') . ' ' .print_r($argv,true));
} else {
 // This part is only executed in the parent
 exit(0);
}
?>



_______________________________________________

UPHPU mailing list
[email protected]
http://uphpu.org/mailman/listinfo/uphpu
IRC: #uphpu on irc.freenode.net

Reply via email to