Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: flock and read-ahead buffering on input

by dave_the_m (Monsignor)
on Apr 17, 2014 at 21:18 UTC ( [id://1082679]=note: print w/replies, xml ) Need Help??


in reply to flock and read-ahead buffering on input

I find your description of a potential problem hard to follow. What actor are you worried about performing the buffering? The process running your code above? Some other process? The OS?

Your code example looks perfectly safe. Except note that flock doesn't do mandatory locking. Locking the file doesn't stop other processes doing something with the file; it only stops other processes locking the file. So if all processes that access the file are under your control, and you ensure that all such processes do a sysopen/flock before doing anything else with the file, then this code should be safe.

(Except if the file is being accessed over a network, in which case all bets are probably off)

Dave.

  • Comment on Re: flock and read-ahead buffering on input

Replies are listed 'Best First'.
Re^2: flock and read-ahead buffering on input
by dbooth (Novice) on Apr 17, 2014 at 22:09 UTC

    I am worried about perl or the OS performing read-ahead buffering as a result of the sysopen call. I am aware that flock is advisory locking, not mandatory. And I am assuming a local file system.

    Clearly read-ahead buffering is normally done (at least on linux), since this post, for example, discusses controlling linux's read-ahead buffer size, and this post discusses controlling the read-ahead buffer size from perl. Therefore, given that read-ahead buffering is normally done, how can the above code possibly be safe? I am fully prepared to believe that it is, but I have not been able to find any authoritative evidence to support that belief, and I want my code to be safe.

    For example, if flock was guaranteed to invalidate the read-ahead buffer, or if the act of another process writing to the file was guaranteed to immediately invalidate the read-ahead buffer, or if the read-ahead buffer was guaranteed to not be filled until this process did a read on the file handle, then the above code would indeed be safe. Can anyone point me to any evidence that that any of these is true, or any other evidence that indicates why the above code is safe, given the fact that read-ahead buffering normally occurs when reading a file?

      Perl won't do any read-ahead buffering of the file until you perform the first read action. If the kernel performs read-ahead, it has a global picture of the file and and all processes, and will be capable of invalidating any buffered data if necessary.

      So that code is perfectly safe for your needs.

      Dave.

        Thanks, that's the key piece of information that I needed. Unfortunately it is not documented in the sysopen, flock or read documentation. Can you point me to where that is documented?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found