Jump to content

[SOLVED] Forking Hell


GeneralBretch

Recommended Posts

I've spent the best part of a couple of days playing with the pcntl_fork function but, whatever I try, it just doesn't seem to work for me. After trying it in my main project, and getting nowhere, I pared it down and am now testing with a very very simple script and even that doesn't work. I'm beginning to think that there may be a server setting preventing it from working but google has so far not yielded any answers, so I'm hoping this forum will.

 

My script is below, followed by the output that appears in my log file. I've tried various approaches but the child processes never appear to die and the pcntl_wait() function just hangs forever. Has anyone seen this behaviour before and can anyone please provide a suggestion before I tear out what hair I have left.

 

The script

$logFile = fopen("/var/www/html/test.txt", "w");
$pid = pcntl_fork();
if ($pid == -1)
{
    fwrite($logFile, "forking error\n");
}
elseif ($pid)
{
    fwrite($logFile, "In parent\n");
    pcntl_wait($status);
    fwrite($logFile, "exit status:" . pcntl_wexitstatus($status) . "\n");
}
else
{
    fwrite($logFile, "In child ".getmypid()."\n");
    sleep(5);
    exit(1);
}

fclose($logFile);

 

The last output for this script was (and the processes have not died):

In child 31747
In parent

Link to comment
https://forums.phpfreaks.com/topic/150111-solved-forking-hell/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.