Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to apply flock

by Chief of Chaos (Friar)
on Nov 24, 2002 at 10:48 UTC ( [id://215490]=note: print w/replies, xml ) Need Help??


in reply to How to apply flock

Hi,
you can use flock in this way
#!/usr/bin/perl -w use strict; use Fcntl ':flock'; # import LOCK_* constants sub lock { flock(FH,LOCK_EX); # and, in case someone appended # while we were waiting... seek(FH, 0, 2); } sub unlock { flock(FH,LOCK_UN); } my $filename = "plain-db.pdb"; open(FH, ">>$filename") or die "Can't open file ($filename): $!"; lock(); # do what ever you want with it unlock();
Maybe this can help you.

Replies are listed 'Best First'.
Re: Re: How to apply flock
by no_slogan (Deacon) on Nov 25, 2002 at 07:04 UTC
    lock(); # do what ever you want with it unlock();

    If "whatever you want" involves tieing a DB_File, you're in for trouble. Some versions of BerkeleyDB close the file and reopen it, which loses the lock for you. This took me a lot of pain and agony to discover, but fortunately not too much data was corrupted before I figured it out.

    Using a separate lock file is good. The mod_perl guide is good. Heed the other monks.

    Update: If you close the same file you lose the lock, even if you open it through a different filehandle and close that one. Locks exist at the inode level, not the filehandle level.

      Sorry,
      flock works on filehandles, if you close the filehandle
      you're in trouble (the lock will be released). That's right.
Re: Re: How to apply flock
by PodMaster (Abbot) on Nov 25, 2002 at 12:21 UTC
    But only if you want to risk data corruption

    That is the classic perl flock boo-boo. You should never "unlock", just close the file, and the lock will go away.

    Also, do you really need a lock subroutine?

    I didn't respond to this the other day in hopes that somebody else would, and much to my suprise, nobody paid attention.


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://215490]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2024-04-25 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found