March 2002 | Perl Hardware Store | 21 |
$FILE = '/data/hitcounter'; $SEMAPHORE = $FILE . '.lck';
print "Content-type: text/html\n\n";
open S, "> $SEMAPHORE" or die ...;
if (something()) { flock S, LOCK_SH; open F, $FILE or die ...; print while <F>; } else { flock S, LOCK_EX; open F, "> $FILE" or die ...; my $data = qx{some command}; print F $data; }
close F; close S;
This solves all sorts of problems
I had a long conversation last week with a guy trying to lock a DB_File database portably
Believe me, you don't want to hear the details
The upshot: This trick works, and nothing else does
Next | Copyright © 2002 M-J. Dominus |