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

File locking and Storable

by jpfarmer (Pilgrim)
on Apr 04, 2004 at 07:23 UTC ( [id://342432]=perlquestion: print w/replies, xml ) Need Help??

jpfarmer has asked for the wisdom of the Perl Monks concerning the following question:

I've been playing around with Storable recently and I'm having some trouble figuring out how to do effective file locking. I understand that there are lock_ versions of the store, nstore, and retrieve methods, but those only provide locking while the actualy store or retrieve operation is going on. In order to effectively prevent a race condition, I need to be able to open the file, lock it, and maintain that lock while I do all of the reading/writing I need to do.

According to the docs, the fd_ versions of the commands operate on an already-open file, however I don't understand how I should open the files to begin with. I assume I want some version of an open statement, but I don't know how to format it. Also, I'm not sure that solves my file locking problem.

I feel like I'm trying to do something that this module should allow me to do, but I can't figure out how to do it. I would really appreciate any advice you can give me.

Replies are listed 'Best First'.
Re: File locking and Storable
by Aragorn (Curate) on Apr 04, 2004 at 10:03 UTC
    File locking is a hairy subject, and there are many ways to get it wrong. An approach that works really well is called semaphore file locking. In this approach, you don't lock the actual data file, but a different one which acts as a sentinel for the actual data file.

    Dominus has a few slides about this in his Perl Hardware Store talks. The relevant slides start here.

    Arjen

Re: File locking and Storable
by matija (Priest) on Apr 04, 2004 at 08:15 UTC
    I'm not quite sure what while I do all of the reading/writing I need to do means, but the code would look something like this:
    use Fcntl; #contains codes to use for flock ... open(STORE,">$storable_file") || die "Could not open $storable_file: $ +!\n"; flock STORE,LOCK_EX; store_fd \%hash,\*STORE; ... # code code code flock STORE,LOCK_UN; close(STORE); # closing file also unlocks it, but I prefer explicit un +locking

      As I pointed out here, you shouldn't use LOCK_UN, just close the filehandle to the lockfile (in this case it's the data file too).

      Also, you should check the return value of close calls.


      If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
      That way everyone learns.

      flock can fail (die) as can close (warn)
      I'm not quite sure what while I do all of the reading/writing I need to do means

      What I ment was that I need to do more than just read or just write the file, I need to be able to read the file, make changes, then write back. If I lock the conventional way, then I'll open a file handle for read, lock it, release the handle, open a handle for write, lock it, then release the handle. There is a period of time between my two locks where the file could be changed. That means the version of the file in memory would be different then the one on disk (the version in memory would be older).

      If I make changes to the version in memory then write out to disk, I'll clobber changes made to the file after I read it into memory. So I need a lock that will let me have exclusive access to the file until I'm done with it.

Log In?
Username:
Password:

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

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

    No recent polls found