tags:

views:

89

answers:

1

I'm forking with this code:

echo "1. posix_getpid()=".posix_getpid().", posix_getppid()=".posix_getppid()."\n";

$pid = pcntl_fork();
var_dump($pid);
if ($pid == -1) die("could not fork");

if ($pid) {
//parent
echo "2. pid=".$pid.", posix_getpid()=".posix_getpid().", posix_getppid()=".posix_getppid()."\n";
} else {
//child
$sid = posix_setsid();

if ($sid < 0)
exit;

echo "3. pid=".$pid.", posix_getpid()=".posix_getpid().", posix_getppid()=".posix_getppid()."\n";

$fp = fopen("/tmp/testfile", "w");
fwrite($fp, '$data');
fclose($fp);

} 

For some reason sometimes the else block is sent to the browser, and sometimes the if ($pid) block is displayed.

What I'm trying to achieve is to send some response, and then continue processing after the connection with the client has closed. Then finally close after the post-request processing is finished. If I sleep in the child or parent the request hangs there.

Both parts are being executed though, it's just being weird in determining which on is sent to the browser.

Thanks in advance.

A: 

Consider using ignore_user_abort() instead.

Ignacio Vazquez-Abrams
Can I explicitly close the connection too? Because it seems that the connection to client is held until the user aborts.
Chris