Created
November 13, 2009 18:40
-
-
Save waffle2k/234059 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use Fcntl qw(:DEFAULT :flock ); | |
use Getopt::Std; | |
sub usage { | |
print <<USAGE | |
Usage: | |
SCRIPT.pl -f LOCKFILE | |
-l: The Filename which you wish to lock | |
USAGE | |
; | |
exit(0); | |
} | |
my %options; | |
getopts("luhf:",\%options); | |
usage() if $options{h}; | |
usage() unless defined $options{f}; | |
sysopen( FD, "options{f}", O_RDWR|O_EXCL, 0755) | |
or die("Cannot open $options{f}: $!\n" ); | |
unless ( flock ( FD, LOCK_EX | LOCK_NB ) ) { | |
local $| = 1; | |
print "Waiting for lock on filename..."; | |
flock ( FD, LOCK_SH ) or die ( "Can't lock file: $!\n" ); | |
print "\n"; | |
} ## end unless ( flock ( FD, LOCK_EX... | |
print "Received it! Hit Enter to release it..."; | |
my $r = <>; | |
close ( FD ); | |
exit( 0 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment