advisory locking using Storable module

Discussion in 'Perl Misc' started by badarisj@gmail.com, Apr 12, 2006.

  1. Guest

    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";
    }
    , Apr 12, 2006
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> 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.
    Anno Siegel, Apr 12, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timasmith
    Replies:
    4
    Views:
    441
    Bjorn Borud
    Nov 1, 2006
  2. Kenjis Kaan

    Storable module for Activestate 5.6.1??

    Kenjis Kaan, Jun 30, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    124
    Sisyphus
    Jul 1, 2003
  3. Mike G.

    help installing Storable module

    Mike G., Feb 2, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    131
    Ben Morrow
    Feb 3, 2004
  4. Marinos Yannikos
    Replies:
    2
    Views:
    144
    A. Sinan Unur
    Mar 25, 2005
  5. arun
    Replies:
    1
    Views:
    1,376
    Rainer Weikusat
    Mar 8, 2012
Loading...

Share This Page