Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   advisory locking using Storable module (http://www.velocityreviews.com/forums/t897445-advisory-locking-using-storable-module.html)

badarisj@gmail.com 04-12-2006 08:53 PM

advisory locking using Storable module
 
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";
}


Anno Siegel 04-12-2006 09:45 PM

Re: advisory locking using Storable module
 
badarisj@gmail.com <badarisj@gmail.com> wrote in comp.lang.perl.misc:
> 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]

> 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";
> }
>

--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.


All times are GMT. The time now is 05:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.