flock() may not be required - but I'm pretty sure it's damn sensible. As for
using mySQL, well, these routines are part of a simple file-based cache
system (specifically in my case used for caching mySQL data)

Jtjohnston <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> Group: Is flock even necessary?
> Allan: You look like you have Perled. It was necessary for Perl.
> Have you thought of MySQL instead?
>
>
> Alan McFarlane wrote:
>
> > I've just written a couple of simple routines which read an write small
text
> > files to the server:
> >
> > function readData( $filename, &$data )
> > {
> >  if (!$fd = @fopen($filename, "r"))
> >  {
> >   return false;
> >  }
> >
> >  flock($fd, LOCK_SH);
> >  $data = fread($fd, filesize($filename));
> >  fclose($fd);
> >
> >  return true;
> > }
> >
> > function writeData( $filename, $data )
> > {
> >  if (!$fd = @fopen($filename, "w"))
> >  {
> >   return false;
> >  }
> >
> >  flock($fd, LOCK_EX);
> >  fwrite($fd, $data);
> >  fclose($fd);
> >
> >  return true;
> > }
> >
> > Now, the question is... How much time elapses between the fopen()
statement
> > and the flock() statements?
> >
> > Looking at the code, it would appear that two separate threads may call
> > writeData() almost simultaneously giving the following execution:
> >
> >  assume $filename = "sample.txt";
> >
> > Thread #1 - $fd = fopen($filename, "w");  // succeeds
> > Thread #2 - $fd = fopen($filename, "w");  // succeeds (I think)
> > Thread #1 - flock($fd, LOCK_EX);          // Does this succeed or fail?
> > Thread #2 - flock($fd, LOCK_EX);          // fails (I think)
>
> --
> John Taylor-Johnston
> --------------------------------------------------------------------------
---
>   ' ' '   Collège de Sherbrooke:
>  ô¿ô   http://www.collegesherbrooke.qc.ca/languesmodernes/
>    -     Université de Sherbrooke:
>           http://compcanlit.ca/
>           819-569-2064
>
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to