do i need to flock when just reading a file?

G

Guy

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.

Thanks,
Guy
 
N

News123

Just some thoughts.as 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
trunkated.

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
lock.
 
J

Jürgen Exner

Guy said:
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?

Insufficient information. What happens in the standard scenarios:
- concurrent reads are obviously no problem as they don't modify the
file
- 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
file?

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
files.

jue
 
T

Tad J McClellan

Jürgen Exner said:
Insufficient information. What happens in the standard scenarios:
- concurrent reads are obviously no problem as they don't modify the
file
- 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
file?

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.


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>
 
I

Ilya Zakharevich

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?

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,
Ilya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,002
Messages
2,570,261
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top