#!/local/bin/perl # use Getopt::Long; GetOptions(qw(read write debug file:s)); die "No file specified" if (!$opt_file); print "file: $opt_file\n"; open(BLAH, ">$opt_file"); if ($opt_read) { $rc = read_lock('BLAH'); } elsif ($opt_write) { $rc = write_lock('BLAH'); } else { die "No lock method defined."; } print "Return lock - $rc\n" if ($opt_debug); chop($junk=); $rc = clr_lock('BLAH'); print "Return clr - $rc\n" if ($opt_debug); close(BLAH); # # File locking defines sub LOCK_SH { 1; } sub LOCK_EX { 2; } sub LOCK_NB { 4; } sub LOCK_UN { 8; } # # Shared lock on a file - flock() should be okay everywhere (see perlfunc) sub read_lock { my($fh)=shift; print "SH_LOCKing $fh\n" if ($opt_debug); return flock($fh, LOCK_SH()); } sub write_lock { my($fh)=shift; print "EX_LOCKing $fh\n" if ($opt_debug); return flock($fh, LOCK_EX()); } sub clr_lock { my($fh)=shift; print "UN_LOCKing $fh\n" if ($opt_debug); return flock($fh, LOCK_UN()); }