Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: File locking and Storable

by matija (Priest)
on Apr 04, 2004 at 08:15 UTC ( [id://342437]=note: print w/replies, xml ) Need Help??


in reply to File locking and Storable

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

Replies are listed 'Best First'.
Re: Re: File locking and Storable
by BazB (Priest) on Apr 04, 2004 at 09:46 UTC

    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.

Re: Re: File locking and Storable
by Anonymous Monk on Apr 04, 2004 at 10:46 UTC
    flock can fail (die) as can close (warn)
Re: Re: File locking and Storable
by jpfarmer (Pilgrim) on Apr 05, 2004 at 00:18 UTC
    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: note [id://342437]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found