in reply to The necessity of flock-ing files that are only to be read.

As I only plan to read the file - does this indicate I do not need to lock it? It is a static file and will theoretically not be changed ever. Should I use shared locks or are these rendered unnecessary by the fact I never plan to write or append to the file?
If your scripts (or any other programs) don't modify the files you don't need to lock them.

The only reason to (shared-)lock files you are reading is when both:

  1. Someone might be updating the file, and that someone is doing an exclusive lock himself.
  2. Things go wrong when the file you are reading is malformed, for any definition of 'wrong' and 'malformed'.
For instance: say I have a small script which shows the last X lines of a log-file. Now, I only use the script for debugging purposes, so even though the log-file might be updated, I do not need to lock the file because it doesn't matter if the read goes wrong sometimes (in this case the most likely 'misread' will be an incomplete line at the end).

On the other hand, if I use a file for passing data from one program to the other, I really need the data to be consistant, so I should use locking on the read and the writing side.

Hope this clears it up a bit ;-)

-- Joost downtime n. The period during which a system is error-free and immune from user input.

Replies are listed 'Best First'.
Re: Re: The necessity of flock-ing files that are only to be read.
by Nemp (Pilgrim) on Sep 02, 2002 at 15:57 UTC
    Thanks for all the replies especially this one!

    This answer has really helped clear things up for me! I'm implementing shared locks on my file now - just to get in the habit and in case I ever decide I need to update that file in the future.

    I appreciate the help!
    Neil