Next | The Perl Hardware Store | DC.pm Version | 26 |
Semaphore files solve many problems associated with locking
Example: How to lock a Berkeley DB file?
tie %db => 'DB_File', $filename, ... or die ...; open FH, "<&=".tied(%db)->fd or die ...; flock FH, LOCK_EX or die ...;
Complicated and doesn't work
Berkeley DB reads and caches data immediately after the open
By then it's too late to get the lock
Solution: Semaphore file
open S, "> $SEMAPHORE" or die ...; flock S, LOCK_EX or die...; tie %db => 'DB_File', $filename, ... or die ...; ... untie %db; close S;
Problem solved
Next | Copyright © 2003 M. J. Dominus |