a Post-script to flocking question

Discussion in 'Perl Misc' started by Dick Rosser, Nov 13, 2003.

  1. Dick Rosser

    Dick Rosser Guest

    Sorry, I also meant to ask if there is any
    error trapping with flock. If a second person
    accesses the database befor the first persons lock
    is in place, is the database corrupted. How does one
    work around this?

    Regards
    Dick Rosser
    webmasterone@kidwatch-uk,net
     
    Dick Rosser, Nov 13, 2003
    #1
    1. Advertising

  2. Also sprach Dick Rosser:

    > Sorry, I also meant to ask if there is any
    > error trapping with flock. If a second person
    > accesses the database befor the first persons lock
    > is in place, is the database corrupted. How does one
    > work around this?


    You don't, but flock() does. This is exactly the scenario it is used
    for. Since locks are usually only advisable, both parties accessing the
    database have to use flock(). If there is already a lock on it, the
    other process will block until the lock is released. You can also do
    non-blocking locks in which case flock() returns a false value if the
    file is already locked:

    use Fcntl qw:)flock);
    ...
    flock FH, LOCK_EX|LOCK_NB or die $!;

    This can be used for allowing the waiting processes do something useful
    in the meantime:

    while (! flock FH, LOCK_EX|LOCK_NB) {
    warn $!;
    # do some useful stuff here
    }
    # and now we have the lock and can work with the file

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Nov 13, 2003
    #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. Wet Basement
    Replies:
    1
    Views:
    2,530
    BobMonk
    Jul 15, 2003
  2. Rajat
    Replies:
    3
    Views:
    705
    Jorgen Grahn
    Jan 8, 2010
  3. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    565
    Jan Kaliszewski
    Jan 7, 2010
  4. \Dandy\ Randy

    Final "Flocking" Script

    \Dandy\ Randy, Aug 5, 2003, in forum: Perl Misc
    Replies:
    13
    Views:
    146
    Tad McClellan
    Aug 8, 2003
  5. \Dandy\ Randy

    Flocking Advise

    \Dandy\ Randy, Sep 9, 2003, in forum: Perl Misc
    Replies:
    8
    Views:
    85
Loading...

Share This Page