I need to use flock to prevent two processes from attempting to read/write a GDBM file at the same time. Following this example: http://www.nntp.perl.org/group/perl.beginners/2001/09/msg11124.html and this one: http://docstore.mik.ua/orelly/perl/cookbook/ch14_06.htm I wrote the following code:
use strict; use GDBM_File; use Carp qw(confess); use Fcntl qw(:flock); my %term_doc_count; my $db = tie(%term_doc_count, 'GDBM_File', "resources/term_docCount", +GDBM_READER, 0666) or confess("Unable to tie GDBM db : $!"); my $fd = $db->fd(); open(DB_FH, "<&=$fd") or confess "Could not open file: $!"; unless (flock (DB_FH, LOCK_SH | LOCK_NB)) { print "$$: CONTENTION; can't read during write update! Waiting for read lock ($!) .... \n"; unless (flock (DB_FH, LOCK_SH)) { die "flock: $!" } } print "$$: Read lock granted\n"; untie %term_doc_count; close(DB_FH); flock(DB_FH, LOCK_UN);
However, I am getting the following error: fd is not a valid GDBM_File macro at test.pl line 12
I can't tell from the GDBM_file documentation here: http://search.cpan.org/~flora/perl-5.14.2/ext/GDBM_File/GDBM_File.pm, how I can get the file descriptor. Please, any ideas?
In reply to GDBM_File file descriptor for use with flock by sdray
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |