0

where in I need to fork a process. Then when their task completes, the parent process should go ahead and do some other work. How can this be done? Here's what I have done

$sql = "call some_sample_proc()";
$res = $dbConn->query($sql);
$i=0;
$memcacheData = array();
while($row = $dbConn->fetch_assoc()) {
    $value = $row['col1'].$row['col2'].$row['col3'];
    $memcachceData[$i++] = $value;
}
for($i=0; $i<5; $i++) {
    $pid = pcntl_fork();
    if($pid == 0) {
        $im = $i;
        echo "forked process $i \n";   //  -- 1
        exit;
    } else if($pid == -1) {
        echo "Could not fork \n";
    }
}
while (pcntl_waitpid(0, $status) != -1) {
    $status = pcntl_wexitstatus($status);
    echo "Child $status completed $im\n ";
    $childCompleteCount ++;
}
echo "completion count $childCompleteCount\n";
if($childCompleteCount == 5) {
     echo "All finished\n";
     //exit; //if this is uncommented, ONLY THEN the program finishes 
}

When I execute the program with this snippet of code I get this output...

forked process 0
forked process 1
forked process 3
forked process 2
forked process 4
Child 0 completed 0
Child 0 completed 0
Child 0 completed 0
Child 0 completed 0
Child 0 completed 0
completion count 5
All finished

But after printing completion count 5 program seems to hang. It doesnt return to prompt. So now that we understand that all children (forked) processes have finished, how do we resume the further execution?

Shades88
  • 7,934
  • 22
  • 88
  • 130
  • no snippet. cant write the whole code – Shades88 Nov 19 '12 at 13:58
  • The code as shown shouldn't exhibit this issue, so perhaps it's caused somewhere else. – Ja͢ck Nov 19 '12 at 14:00
  • actually, there's nothing special after the block of code that I have presented here. Some db connections are closed, some log entries are made and that's it. And it all works if I chose not to do this forking. It hangs, doesn't return to prompt – Shades88 Nov 19 '12 at 14:03

0 Answers0