Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: flock() ..I really need a hand .. please :)

by a (Friar)
on Mar 20, 2001 at 09:59 UTC ( [id://65633]=note: print w/replies, xml ) Need Help??


in reply to flock() ..I really need a hand .. please :)

Well, this probably won't be helpful, but ...
flock is not going to save you if the 'other' apps won't play nice. Likewise, unless all your apps respect the SEMAPHORE flag (e.g.:
while ( -s $SEMAPHORE ) { print STDERR "lock found waiting\n" if $debug > 10; sleep 5; }
flocking it won't do much. Of course, this implies that $SEMAPHORE is a well known name (/tmp/myapp.lck), not /tmp/mlock$$.lck - unless other versions can predict the name and check if it exists, (I'm guessing $file is the useful name and the file that gets 'wiped' now and then) your lock/semaphore isn't going to help. It seems your code would allow 2 instances to mess w/ $file if they each had different $tempfile instances. It should be that they have one $SEMAPHORE to work w/, check the existence of, etc.

Sorry, I don't recall your other posts, but you might also do your self some good by having the 'rename' preceeded by a debug "about to rename $tempfile to $file (I'm $this instance)" so you can see who is doing the wiping in hopes of pinpointing where your lock/semaphore goes a stray. As I read it, though your flocking isn't really giving you exclusive access for multiple instances of that code, or for anybody else, unless they're using the same $tempfile.

a

Replies are listed 'Best First'.
Re: Re: flock() ..I really need a hand .. please :)
by Anonymous Monk on Mar 20, 2001 at 13:47 UTC
    I think I've found were I am going wrong. Thanks a for your effort of a reply, after re-reading a few times everything you said, I am wrong, my $SEMAPHORE file is not the same all the time.

    I think what I was doing was concentrating so hard on KM's example of renaming the "locked file" the same name as the file getting written to, that I neglected to think of the first lesson I learned about flock(). That flock() works if the file in question shares the same locking functions throughout the code.

    What I think I'm doing wrong, and please correct me if I'm not right, but when I was reading file A to spit out the contents, I was using a shared lock (LOCK_SH) and I named my $SEMAPHORE file the same name as file A but with .lck at the end. So sometime during the code I had to write to my TEMP file B and named my $SEMAPHORE file "file B.lck" So in essence the two $SEMAPHORE file names were different.

    Is this were my mistake lays? If I'm assuming correctly I have to name my TEMP $SEMAPHORE file name the same as "file A" Does this make sense so far? I hope I'm explaining myself correctly :) Here is a small example of what I was doing wrong I think:

    ########################################## # Reading $Afile = "A.txt"; $SEMAPHORE = $Afile . '.lck'; open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; flock(S, LOCK_SH) or die "flock() failed for $SEMAPHORE: $!"; open (FH, "$Afile") or die "Can't open $Afile: $!"; while (<FH>) { ## do_something; } close FH; close S; ##########################################- wrong way # Writing $Bfile = "B.txt"; $SEMAPHORE = $Bfile . '.lck'; ### Using $Bfile as $SEMAPHORE file name open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!"; open (TEMP, ">$Bfile") or die "Can't open $Bfile: $!"; open (FH, "$Afile") or die "Can't open $Afile: $!"; while (<FH>) { ## do_something; } close FH; close TEMP; rename($Bfile,$Afile); close S; ########################################## -right way? # Writing $Bfile = "B.txt"; $SEMAPHORE = $Afile . '.lck'; ### Should be using $Afile as $SEMAPHORE + file name etc..
    So am I safe to assume that this would wipe my file contents? Because when one process opened my main file for reading my other process may have been re-writing the TEMP file then renaming it, and my main file was not flocked. I think I'm confusing myself :/ but I hope I'm right.??!

    Thank you again, and thanks AgentM for the reply too. I'll give that module a try :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-28 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found