Re: file lock unnecessary?

[email protected] (Greg Jetter)
Newsgroups perl.beginners.cgi
Organization Lazy Mountain Computers
Message-ID <[email protected]>
On Wednesday April 25 2007 6:14 am, Varjü Tamás wrote:
> Hello everyone!
>
> I have written a perl script which opens a file for reading and writing.
> I tested what happens if the script is run in multiple instances by the
> webserver.  I put a sleep command between seeking writing position in
> the file and actual writing.  I filled the web form in two different
> tabs of my browser and sent it.
>
> My impression was that the following happens:
> - first instance opens the file,
> - first instance seeks the writing position,
> - first instance goes sleeping,
> - second instance opens the same file,
> - second instance seeks the writing position (different from the above
> one),
> - second instance goes sleeping,
> - first instance wakes up,
> - first instance writes the file,
> - first instance closes the file,
> - second instance wakes up,
> - second instance writes the file,
> - second instance closes the file.
>
> After that procedure both changes has appeared in the file.
>
> Does it mean, that on this system the multiple instance writing of a
> file is handled well and I do not have to worry about locking the file
> as long as the different instances write different positions in the
> file?  Even the system administrator had the tip, that only the last
> change will appear.
>
> If I want to be sure that multiple instance writing will work, is append
> a better choice?  I have read that on the archive of this list.
>
> Thanks

you should use flock to  lock the file , so one process can work on it at a 
time , if you don't eventually  a race way condition will occur  and  you 
will see a lock up happen , or   if your luck  one process will  overwrite 
the other  and you will just loose data..  either way  it's  just smart 
programming to lock a file  your working on especially if  it's happening 
under CGI conditions.

Greg
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.