Re: Re: Fork and zombies

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



>
> > Yeah, something like that. The connection is definitely closed when the
>> > child exits.
>> >
>>
>> I can confirm this. you definitely need to open a connection for each
>> child process.
>> if you require a db connection in the parent process, you should close
>> it before forking (and then reopen it afterwards if you still need it).
>>
>
> I thought that too, but this code seems to work, which seems to imply that
> the child doesn't kill the existing db connection.
>
> $conn = mysql_connect($sharedAppsDbHost, $sharedAppsDbUser,
>                                  $sharedAppsDbPass, true);
>
> foreach ($things as $thing) {
>   temp($thing);
> }
>
> function temp($thing) {
>   global $conn;
>   extract(getInfo($thing)); // this function call uses a shared db
> connection
>   mysqlSelectDb($dbName, $conn); // dbName is a variable gotten from the
> above call
>   $result = mysql_query("SELECT COUNT(*) FROM Users",
>                         $conn);
>   $row = mysql_fetch_array($result, MYSQL_BOTH);
>   echo "$row[0]\n";
>   $pid = pcntl_fork();
>   if ($pid == -1) {
>     die("could not fork");
>   } else if ($pid) {
>     // parent, return the child pid
>     echo "child pid $pid waiting\n";
>     pcntl_waitpid($pid, $status, WNOHANG);
>     if (pcntl_wifexited($status)) {
>       echo "finished [$status] waiting\n";
>       return;
>     }
>   } else {
>     echo "child sleeping\n";
>     sleep(3);
>     echo "child done\n";
>     exit;
>   }
> }
>
> ==============
> My main problem here is that I have a set of helper functions (getInfo is
> one of them) that uses a global db connection that exists in that helper
> script.  Otherwise, I have to rewrite the function to create a new
> connection every time, which I'd like not to.
>
> Waynn
>

Whoops, I spoke too soon, I think the sleep(3) causes the child not to exit
before the parent.  If instead I don't pass WNOHANG to the waitpid command,
it does error out.  So it looks like your theory is correct, and I still
have that problem.  I guess the only solution is to rewrite the functions to
use a new db connection every time?  Or maybe I should just sleep the child
thread for long periods of time and hope the parent thread finishes in time
(which admittedly seems really hacky)?

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux