advisory locking using Storable module

B

badarisj

folks,

i wrote a small test program to test the advisory locking
using the routines from Storable module.

in my program, the parent process gets
a shared-lock by invoking 'lock_retrieve' function.
the child process attempts to get the exclusive lock
by calling lock_store.

i expected the child's lock_store be blocked
till the parent exits and NOT see the message with ERROR prefix.

but to my surprise, i DO see the message with ERROR prefix
BEFORE the parent process exited.

what am i missing here?

thanks,
-badari


output on my screen:
================

squirrel 23 % ./storable_locking
The parent process got shared/read lock
ERROR: How could i store if there was a shared lock gotten by my parent
<the prompt takes quite sometime to come back owing to sleep>


the test program:
=============

#!/usr/cisco/bin/perl5.8 -w

use Storable qw(lock_store lock_nstore lock_retrieve);

my $struct =
{
"field1" => "val1"
};
my $file = 'struct_store';
# store the struct
lock_nstore $struct, $file;

if ( my $pid = fork() ) {
# parent
$struct = lock_retrieve $file;
print "The parent process got shared/read lock\n";
sleep(1000);
print "parent got out of sleep and exiting.";
} else {
#child
sleep(10);
$struct->{field1} = 'new val';
my $hashref = lock_store $struct,$file;
print "ERROR: How could i store if there was a shared lock gotten
by my parent\n";
}
 
A

Anno Siegel

folks,

i wrote a small test program to test the advisory locking
using the routines from Storable module.

in my program, the parent process gets
a shared-lock by invoking 'lock_retrieve' function.
the child process attempts to get the exclusive lock
by calling lock_store.

i expected the child's lock_store be blocked
till the parent exits and NOT see the message with ERROR prefix.

but to my surprise, i DO see the message with ERROR prefix
BEFORE the parent process exited.

what am i missing here?

Storable certainly doesn't lock files longer than needed. In particular,
the locks acquired by lock_retrieve and lock_store are released when the
functions return. To see the lock mechanism at work, you'd need to delay
the lock- and retrieve-functions themselves. Storable has a hook mechanism
that should allow that.

Anno

[code retained for reference]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top