Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

RE: File Locking

by KM (Priest)
on May 22, 2000 at 07:19 UTC ( #14140=note: print w/replies, xml ) Need Help??


in reply to File Locking

To see an example of flock() in action, try running this script in a few consoles side by side.

#!/usr/bin/perl -wT
use Fcntl qw(:flock);
my $file = 'test_lock.txt';
my $SEMAPHORE = $file . '.lck';
open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!";
flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!";
open (FH, ">>$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;

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 :)

Cheers,
KM

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://14140]
help
Chatterbox?
[Discipulus]: sometimes the client dies for extra content </chatter><span class="error" style="color: #cc0000">Server Error (Error ID 9162
[Discipulus]: the span is considered extra content by the parser
[Corion]: Mhmm - that would be some internal error, but I can't look into that right now as I don't have access to the logs from here ;)
[Discipulus]: ah, but it can be skipped by the client (?)
[Corion]: I think so, but I wonder what error might be there...
[Discipulus]: raining in germany? ;=)

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2017-07-25 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (366 votes). Check out past polls.