tags:

views:

3635

answers:

10

What is the best way to create a lock on a file in Perl?

Is it best to flock on the file or to create a lock file to place a lock on and check for a lock on the lock file?

A: 

Use the flock Luke.

Edit: This is a good explanation.

mk
The links are great
Ryan P
A: 

flock creates Unix-style file locks, and is available on most OS's Perl runs on. However flock's locks are advisory only.

edit: emphasized that flock is portable

crosstalk
While this is helpful back information it does not really answer the question
Ryan P
+5  A: 

If you end up using flock, here's some code to do it:

use Fcntl ':flock'; # Import LOCK_* constants

# We will use this file path in error messages and function calls.
# Don't type it out more than once in your code.  Use a variable.
my $file = '/path/to/some/file';

# Open the file for appending.  Note the file path is quoted
# in the error message.  This helps debug situations where you
# have a stray space at the start or end of the path.
open(my $fh, '>>', $file) or die "Could not open '$file' - $!";

# Get exclusive lock (will block until it does)
flock($fh, LOCK_EX) or die "Could not lock '$file' - $!";

# Do something with the file here...

# Do NOT use flock() to unlock the file if you wrote to the
# file in the "do something" section above.  This could create
# a race condition.  The close() call below will unlock the
# file for you, but only after writing any buffered data.

# In a world of buffered i/o, some or all of your data may not 
# be written until close() completes.  Always, always, ALWAYS 
# check the return value of close() if you wrote to the file!
close($fh) or die "Could not write '$file' - $!";

Some useful links:

In response to your added question, I'd say either place the lock on the file or create a file that you call 'lock' whenever the file is locked and delete it when it is no longer locked (and then make sure your programs obey those semantics).

Chris Bunch
The code here was excellently helpful. As well as the links!
Ryan P
+4  A: 

CPAN to the rescue: IO::LockedFile.

Gary Richardson
Ahhh!!! Object oriented Perl eek. The IO::LockedFile is implemented using the flock functions
Ryan P
A: 

My goal in this question was to lock a file being used as a data store for several scripts. In the end I used similar code to the following (from Chris):

open (FILE, '>>', test.dat') ; # open the file 
flock FILE, 2; # try to lock the file 
# do something with the file here 
close(FILE); # close the file

In his example I removed the flock FILE, 8 as the close(FILE) performs this action as well. The real problem was when the script starts it has to hold the current counter, and when it ends it has to update the counter. This is where Perl has a problem, to read the file you:

 open (FILE, '<', test.dat');
 flock FILE, 2;

Now I want to write out the results and since i want to overwrite the file I need to reopen and truncate which results in the following:

 open (FILE, '>', test.dat'); #single arrow truncates double appends
 flock FILE, 2;

In this case the file is actually unlocked for a short period of time while the file is reopened. This demonstrates the case for the external lock file. If you are going to be changing contexts of the file, use a lock file. The modified code:

open (LOCK_FILE, '<', test.dat.lock') or die "Could not obtain lock";
flock LOCK_FILE, 2;
open (FILE, '<', test.dat') or die "Could not open file";
# read file
# ...
open (FILE, '>', test.dat') or die "Could not reopen file";
#write file
close (FILE);
close (LOCK_FILE);
Ryan P
+3  A: 

Have you considered using the LockFile::Simple module? It does most of the work for you already.

In my past experience, I have found it very easy to use and sturdy.

Swaroop C H
This looks a bit more complicated than the flock structure. I don't know about flocks concurrency issues but this specifically notes the race condition possibilities of NFS which we will be using.
Ryan P
+5  A: 

I think it would be much better to show this with lexical variables as file handlers and error handling. It is also better to use the constants from the Fcntl module than hard code the magic number 2 which might not be the right number on all operating systems.

    use Fcntl ':flock'; # import LOCK_* constants

    # open the file for appending
    open (my $fh, '>>', 'test.dat') or die $!;

    # try to lock the file exclusively, will wait till you get the lock
    flock($fh, LOCK_EX);

    # do something with the file here (print to it in our case)

    # actually you should not unlock the file
    # close the file will unlock it
    close($fh) or warn "Could not close file $!";

Check out the full documentation of flock and the File locking tutorial on PerlMonks even though that also uses the old style of file handle usage.

Actually I usually skip the error handling on close() as there is not much I can do if it fails anyway.

Regarding what to lock, if you are working in a single file then lock that file. If you need to lock several files at once then - in order to avoid dead locks - it is better to pick one file that you are locking. Does not really matter if that is one of the several files you really need to lock or a separate file you create just for the locking purpose.

szabgab
You should check `close` anyway. You can't do much, but you can at least tell the user and bail out, instead of silently continuing to run as if nothing had happened.
Aristotle Pagaltzis
+2  A: 

Ryan P wrote:

In this case the file is actually unlocked for a short period of time while the file is reopened.

So don’t do that. Instead, open the file for read/write:

open my $fh, '+<', 'test.dat'
    or die "Couldn’t open test.dat: $!\n";

When you are ready to write the counter, just seek back to the start of the file. Note that if you do that, you should truncate just before close, so that the file isn’t left with trailing garbage if its new contents are shorter than its previous ones. (Usually, the current position in the file is at its end, so you can just write truncate $fh, tell $fh.)

Also, note that I used three-argument open and a lexical file handle, and I also checked the success of the operation. Please avoid global file handles (global variables are bad, mmkay?) and magic two-argument open (which has been a source of many a(n exploitable) bug in Perl code), and always test whether your opens succeed.

Aristotle Pagaltzis
Does seeking back to the beginning actually overwrite the contents of the file? That was my specific issue. I am testing with the die I was just trying to keep the code simple in the example. I nevere thought of the exploit of the 2 arg open thanks I will keep that in mind.
Ryan P
Yes, it will overwrite the contents. But I forgot to mention that you need to truncate the file before closing it, to ensure that if the new contents are shorter, the file doesn’t end up with trailing garbage.
Aristotle Pagaltzis
+3  A: 
use strict;

use Fcntl ':flock'; # Import LOCK_* constants

# We will use this file path in error messages and function calls.
# Don't type it out more than once in your code.  Use a variable.
my $file = '/path/to/some/file';

# Open the file for appending.  Note the file path is in quoted
# in the error message.  This helps debug situations where you
# have a stray space at the start or end of the path.
open(my $fh, '>>', $file) or die "Could not open '$file' - $!";

# Get exclusive lock (will block until it does)
flock($fh, LOCK_EX);


# Do something with the file here...


# Do NOT use flock() to unlock the file if you wrote to the
# file in the "do something" section above.  This could create
# a race condition.  The close() call below will unlock it
# for you, but only after writing any buffered data.

# In a world of buffered i/o, some or all of your data will not 
# be written until close() completes.  Always, always, ALWAYS 
# check the return value on close()!
close($fh) or die "Could not write '$file' - $!";
John Siracusa
A: 

Here's my solution to reading and writing in one lock...

open (TST,"+< readwrite_test.txt") or die "Cannot open file\n$!";
flock(TST, LOCK_EX);
# Read the file:
@LINES=<TST>;
# Wipe the file:
seek(TST, 0, 0); truncate(TST, 0);
# Do something with the contents here:
push @LINES,"grappig, he!\n";
$LINES[3]="Gekke henkie!\n";
# Write the file:
foreach $l (@LINES)
{
   print TST $l;
}
close(TST) or die "Cannot close file\n$!";
Steven de Brouwer