do i need to flock when just reading a file?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
A quick question...
I'm using Perl to read and also write to a text file that was originally
created in Notepad. Each line in the file is a record. Each record contains
about 4 or 5 tab-separated fields.  It will mostly be read, but written to
on occasion.   Do i need to flock the file before reading it, or just when I
am writing to it?

PS. I remember a Perl script I had created about 6 or 7 years ago for an
on-line auction of about 100 items.  It worked fine for a week, until about
5 minutes before the auction was to end. The text datafiles got messed up a
bit when about 200 people tried to access the site at the same time to put
their final bids. I had forgotten to flock the file before writting to them.


Re: do i need to flock when just reading a file?

Just some I personally never had a setup, which was
sensitive to simultaneous writes / reads of a file:

If writing the file means trunkating it and rewriting i, then I would
also lock reading it.

It might happen, that you read the file and while reading it it could be

Even if writing the file would mean creating a new file with a new inode
(for unix file systems), then it could happenm that you read a partially
created file.

If writing just means appending to the file and the amount, that you
append is smaller than the write block size, then you shouldn't have to

Guy wrote:
Quoted text here. Click to load it

Re: do i need to flock when just reading a file?

Quoted text here. Click to load it

Insufficient information. What happens in the standard scenarios:
- concurrent reads are obviously no problem as they don't modify the
- what about concurrent writes? Are they possible in your scenario (it
solely depends on how you implemented your system and also which file
write logic your OS uses)? If yes, how are they handled? Is it possible,
that one write 'wins'? Or that they overwrite each others changes (in
case of update instead of recreating the file)?
- what about concurrent read and write operations? Could there ever be a
scenario, where a file has been written partially (because the process
hasn't finished writing yet) and a read is intitiated on that incomplete

Those are some of the questions you will have to answer first based on
the architecture and implementation of your program as well as the OS
and file system you are using.  

BTW: this has little to do with Perl, you would have the same issues in
any programming language if you are doing concurrent processing of


Re: do i need to flock when just reading a file?

Quoted text here. Click to load it

By now you may want to reconsider your plan to use files as
a "poor man's database".

Use a Real Database and most or all of those questions fall away
as any RDBMS worth it's salt will handle concurrent access for you.

... so now you need a book on the DBI too.  <grin>

Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

Re: do i need to flock when just reading a file?

Quoted text here. Click to load it

As other people said, not enough information.  Keep in mind that on
contemporary OSes, file locking may be better designed than on Unices.
Some C runtimes may even DEFAULT to write-locking when opening a file
for read...

Hope this helps,

Site Timeline