Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Flock and Destroy

by svsingh (Priest)
on Oct 10, 2003 at 20:47 UTC ( #298415=note: print w/ replies, xml ) Need Help??


in reply to •Re: First Time Untainting Data
in thread First Time Untainting Data

There's no point in flocking a file that you destroy before you obtain a flock.

Thanks for your comments. From what I learned in Order of flock and open, I cannot lock the file before I open (and destroy) it.

Here, I was flocking the file to handle the case where two users submit the form data at the same time. I tried using flock with a test script and it seems to protect the temp file as it's being written. Won't that happen in this script as well? (I know what I'm observing, but my test conditions are fairly controlled. I'm happy to defer to your experience.)


Comment on Flock and Destroy
•Re: Flock and Destroy
by merlyn (Sage) on Oct 11, 2003 at 15:23 UTC
    No, you've got a destruction going on. Consider:
    • process 1 opens for create - file deleted
    • process 1 flocks - and continues
    • process 1 writes its data
    • process 2 opens for create - blam, process 1 data is gone
    • process 1 closes, releasing the flock
    • process 2 flocks - and continues
    • process 2 writes its data
    • process 2 closes
    I guess if your goal is to have only the most recent data, you've succeeded, but you didn't need to do the flock for that... you can just leave the flocks entirely out.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

(z) Re: Flock and Destroy
by zigdon (Deacon) on Oct 13, 2003 at 15:21 UTC
    The problem merlyn is trying to point out is that when you open the file for writing (open (FH, ">file")), you already deleted the contents of the file regardless of any locks. From perlopentut:
      To get an exclusive lock, typically used for writing, you have to be careful. We "sysopen" the file so it can be locked before it gets emptied. You can get a nonblocking version using "LOCK_EX | LOCK_NB".
      use 5.004; use Fcntl qw(:DEFAULT :flock); sysopen(FH, "filename", O_WRONLY | O_CREAT) or die "can't open filename: $!"; flock(FH, LOCK_EX) or die "can't lock filename: $!"; truncate(FH, 0) or die "can't truncate filename: $!"; # now write to FH
    Hope that helps!

    -- zigdon

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-09-19 16:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (143 votes), past polls