Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.