Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Flock a file when just reading it?

by Anonymous Monk
on Jun 16, 2005 at 08:45 UTC ( [id://467203]=perlquestion: print w/replies, xml ) Need Help??

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?

Replies are listed 'Best First'.
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\

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

    CU
    Robartes-

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://467203]
Approved by robartes
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found