You are here: Re: Two simultaneous write accesses to a text file « PHP Programming Language « IT news, forums, messages
Re: Two simultaneous write accesses to a text file

Posted by Jerry Stuckle on 03/09/07 03:43

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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация