Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Lock File

by eyepopslikeamosquito (Chancellor)
on Nov 02, 2010 at 20:58 UTC ( #869096=note: print w/ replies, xml ) Need Help??


in reply to Lock File

Yes, use flock. And strict, warnings and 3-argument open. Some sample locking code follows. Adjust according to your needs.

use strict; use warnings; use Fcntl ':flock'; # import LOCK_* constants open(my $fhlock, '>', $somelockfile) or die "Error: open '$somelockfil +e': $!"; # Note: process will block at this point until the lock is acquired. flock($fhlock, LOCK_EX) or die "Error: flock '$somelockfile': $!"; # Lock is now held until $fhlock is closed. # Note that even if this program crashes or is killed, $fhlock will # be closed by the OS and the lock released. # ... # Release the lock simply by closing the file handle. close $fhlock or die "Error: close '$somelockfile': $!";


Comment on Re: Lock File
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (14)
As of 2015-07-29 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (268 votes), past polls