|  | Posted by Jerry Stuckle on 03/09/07 05:57 
Malcolm Dew-Jones wrote:> Jerry Stuckle (jstucklex@attglobal.net) wrote:
 > : Malcolm Dew-Jones wrote:
 > : > andy.z@blueyonder.com wrote:
 > : >
 > : > : If 2 people try to access the same text file at the same time to write to
 > : > : it - what happens in PHP ?
 > : >
 > : > : What I mean is - presumably the first will be ok -
 > : > : But what will the second person actually see in his browser?
 > : > : And what will php do about it if not specifically programmed to deal with
 > : > : it
 > : >
 > : > : ie whats the default behaviour?
 > : >
 > : > At this point it may be useful to know that if you open a file in _append_
 > : > mode on unix/linux then each write statement writes correctly as a single
 > : > "unit" at the _current_ end of the file, even if multiple programs are
 > : > writing to the file and continually changing the size of the file.
 > : >
 > : > That means that if multiple processes all write one line at a time to
 > : > something like a log file then the log file grows one line at a time.
 > : > The lines will be in the order they were written, but will not otherwise
 > : > interfere with each other.  Indeed this is how a basic unix log file
 > : > works.
 > : >
 > : > HOWEVER that only helps with simple text files where you can append things
 > : > at the end.  If you have something like an HTML file then the updates have
 > : > to happen in the middle of the file (before the </body></html> for
 > : > example.
 > : >
 > : > In that case you need to lock the file before you modify it.  You have to
 > : > do that yourself.  There are lots of examples of what you have to do so I
 > : > won't bother trying to remember the exact details here and now.
 >
 > : This is only true if there is no buffering involved.
 >
 >
 > : If there is buffering (as there almost always is), anything can happen.
 > :   If for instance, one script tries to write 800 bytes with a 512 byte
 > : buffer, the first 512 bytes (only) may be written.  And the second
 > : script may get in between the first 512 bytes and the remaining 288
 > : bytes to write its data.
 >
 >
 > : Even worse - to append to a file, the system must read the file into the
 > : buffer.  If this takes 500 bytes, the next 12 bytes can be written - and
 > : the rest buffered.  Then the second script might put data after the
 > : first 12 bytes but before the remaining data.
 >
 > man open(2)
 >  (snip)
 >  O_APPEND
 >                The  file is opened in append mode. Initially, and
 >                before each write, the file pointer is  positioned
 >                at  the  end  of  the  file,  as  if  with  lseek.
 >
 >                O_APPEND may lead to corrupted files on  NFS  file
 >                systems if more than one process appends data to a
 >                file at once.  This is because NFS does  not  sup-
 >                port appending to a file, so the client kernel has
 >                to simulate it, which can't be done without a race
 >                condition.
 >
 > Corruption on * NFS * is explicitly mentioned because that is * not * the
 > normal result of using append mode on unix/linux (until a system wide
 > limit is reached which is on the order of 30,000 bytes).
 >
 > The file routines of other languages seem to have no problem with multiple
 > processes using append mode against the same file, though I suppose that
 > the php file routines could do something that inteferes.
 >
 
 Keep believing that.  But don't come running here when you get a
 corrupted file.  It happens.  The time gap is very small (unless the
 server is very heavily loaded).  But it does exist.  And it exists on Linux.
 
 --
 ==================
 Remove the "x" from my email address
 Jerry Stuckle
 JDS Computer Training Corp.
 jstucklex@attglobal.net
 ==================
 [Back to original message] |