-
-
Save hirose31/93cc87bc884716deae8f17c265b11646 to your computer and use it in GitHub Desktop.
This file contains 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/local/bin/perl5 | |
# | |
# Time-stamp: <1999-07-04 19:15:55 hirose31> | |
# | |
# flock() を使わなくても壊れないカウンタ | |
# つっても排他制御してるんだけどね。 | |
# | |
BEGIN: { $|++; } | |
#$SIG{INT} = \&finish; # fixme ほかにもいろいろ | |
my $cfile = 'n.dat'; | |
my $cfiletmp = "$cfile.$$"; | |
sub alarm_h { | |
alarm 0; | |
print "rest alarm and do next."; | |
if ( glob("n.dat*") ) { | |
open T, ">>$cfile"; close T; | |
} else { | |
exit 1; | |
} | |
} | |
#$SIG{ALRM} = \&alarm_h; | |
#alarm 5; | |
while ( ! rename($cfile, $cfiletmp) ) { | |
# rename $cfile, $cfiletmp and last; | |
# glob("n.dat*") or die $!; | |
print "."; | |
} | |
#alarm 0; | |
print "open.\n"; | |
# here we go! | |
open R, "+<$cfiletmp" or die "$!"; | |
#sleep 1; | |
seek R, 0, 0; | |
my $c = <R>; | |
chomp $c; | |
$c++; | |
print "$c\n"; | |
#<STDIN>; | |
#sleep 1; | |
seek R, 0, 0; | |
print R $c; | |
close R or die "$!"; | |
#<STDIN>; | |
#sleep 1; | |
&finish(); | |
### | |
sub finish { | |
#print "f\n"; | |
rename $cfiletmp, $cfile or die "$!"; | |
exit; | |
} | |
# vi: set ts=4 sw=4: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment