Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
hi, I was wondering if you need to flock a file that your just going open up to read. This file has only 1 line that contains a text. However, this file is opened up everytime it connects to a MySQL db. Meaning for my site, everytime a db connection is made, which is quite frequent, then several perl scripts will read this file. (its the file that containts the mysql db password)
Do I need to flock this file when opening up to read, even though I'm doing no editing?
Re: Flock a file when just reading it?
by gellyfish (Monsignor) on Jun 16, 2005 at 08:51 UTC
|
If nothing else is ever going to be writing to the file concurrently with you program then you don't really need to lock the file no, however if you can envisage that a time might come when you have a program to edit the file then you might want to take out a shared lock while you remember and then remember to take out an exclusive lock when you write to the file in your future program :-)
/J\
| [reply] |
Re: Flock a file when just reading it?
by robartes (Priest) on Jun 16, 2005 at 08:52 UTC
|
If you are only reading in the file *and no one else is writing in it at the same time*, you do not have to lock it.
In fact, you would only need to lock anything for read when 1) your read is non-atomic and 2) the data you're reading might be changed by a writer during your read. I'd say that in your case neither of these conditions is true (although you can't be sure of 1) ).
| [reply] |
|