Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Locking a SDBM_File

by esskar (Deacon)
on Mar 05, 2004 at 03:37 UTC ( [id://334113]=note: print w/replies, xml ) Need Help??


in reply to Locking a SDBM_File

use Fcntl qw(:DEFAULT :flock); use SDBM_File; use strict; my $db = tie(%h, 'SDBM_File', 'filename', O_RDWR|O_CREAT, 0666) or die "Couldn't tie SDBM file 'filename': $!; aborting"; flock($db->fd, LOCK_EX); #... undef $db; untie %h;

Replies are listed 'Best First'.
Re: Re: Locking a SDBM_File
by dws (Chancellor) on Mar 05, 2004 at 03:48 UTC
    I once thought that was the correct way to lock an SDBM file, until someone pointed out that there's a timing window
    my $db = tie(%h, 'SDBM_File', 'filename', O_RDWR|O_CREAT, 0666) or die "Couldn't tie SDBM file 'filename': $!; aborting"; --> # right here flock($db->fd, LOCK_EX);
    At first, you'd think this wasn't a hole. But opening an SDBM file (the last time I looked) actually causes some file I/O. So what happens in this window is that two processes have read some bookkeeping data from the file into memory, one process gets the lock and proceeds to do things that side effect bookkeeping, while the other process blocks. When the first process frees the lock, the second process unblocks, but has data in memory that is now inconsistent with what's on disk. Oops.

    The way around this is to use a separate file for locking, and acquire the lock before opening the SDBM file.

    I may be working off of stale info, though. Can someone who has access to SDBM source confirm that significant I/O happens when the file gets opened?

Re: Re: Locking a SDBM_File
by Brett Wraight (Novice) on Mar 05, 2004 at 05:57 UTC
    this code works on DB_FILE but not on SDBM_FILE

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://334113]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-23 19:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found