<?xml version="1.0" encoding="windows-1252"?>
<node id="14140" title="RE: File Locking" created="2000-05-22 03:19:25" updated="2005-07-20 23:19:02">
<type id="11">
note</type>
<author id="12207">
KM</author>
<data>
<field name="doctext">
To see an example of flock() in action, try running this script in a few consoles side by side.&lt;P&gt;
&lt;PRE&gt;
#!/usr/bin/perl -wT
use Fcntl qw(:flock);
my $file = 'test_lock.txt';
my $SEMAPHORE = $file . '.lck';
open(S, "&gt;$SEMAPHORE") or die "$SEMAPHORE: $!";
flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!";
open (FH, "&gt;&gt;$file") or die "Can't open $file: $!";
print "About to write\n";
print FH "I have written ($$)\n";
print "Written\n";
close FH;
print "Going to sleep...\n";
sleep 10;
print "Woken up...\n";
close S;
&lt;/PRE&gt;
&lt;P&gt;
I just wanted to go through this because I see a lot of mistakes that people make with locking files, and show another way of making it all more secure. To make it even more secure, use a database :)
&lt;P&gt;
Cheers,&lt;BR&gt;
KM
</field>
<field name="root_node">
7058</field>
<field name="parent_node">
7058</field>
</data>
</node>
