Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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.)

Replies are listed 'Best First'.
•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?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2021-04-12 18:05 GMT
Find Nodes?
    Voting Booth?

    No recent polls found