#cat config
10
#cat test_lock.pl
#!/bin/env perl
use strict;
use warnings;
print "Content-type: text/html\n\n";
use Fcntl qw(:flock SEEK_END);
my $conf = "config";
open(my $fp,"+<",$conf) or die $!;
my_lock($fp);
my @lines = <$fp>;
foreach $_ (@lines)
{
chomp;
print "$_\n";
$_--;
}
truncate($fp, 0);
seek($fp, 0, 0);
print $fp join($/, @lines), $/;
my_unlock($fp);
close $fp;
################################################
sub my_lock
{
my ($fh) = @_;
flock($fh, LOCK_EX) or die $!;
}
sub my_unlock
{
my ($fh) = @_;
flock($fh, LOCK_UN) or die $!;
}
阅读(717) | 评论(1) | 转发(0) |