Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: When is it safe to move a file?

by Dominus (Parson)
on Jan 14, 2001 at 22:41 UTC ( [id://51785]=note: print w/replies, xml ) Need Help??


in reply to Re: When is it safe to move a file?
in thread When is it safe to move a file?

Says kschwab:
Something like:

while (1) { $mtime=(stat($data_file . $$))[9]; # drop out of the loop if the file hasn't # changed in 5 minutes (perhaps longer # for a wan connection ?) last if (time > $mtime + 300); }

That is a busy-wait loop. Even if the loop runs only once, you have called stat one million times and run up the load on the system for five minutes.

Try it like this:

my $old_mtime = (stat($file))[9]; if (time <= $old_mtime + 300) { while (1) { sleep 300; my $new_mtime=(stat($file))[9]; last if $old_mtime == $new_mtime; $old_mtime = $new_mtime; } }

Replies are listed 'Best First'.
Re: Re: When is it safe to move a file?
by kschwab (Vicar) on Jan 15, 2001 at 03:27 UTC
    Right, I should have slept() in the loop. I'm not sure how one loop runs stat() a million times though :)

    Fixed my code and re-posted. Thanks for the catch.

      Says kschwab:
      I'm not sure how one loop runs stat() a million times though :)
      Your loop runs for at least 300 seconds. If it runs 3,300 times per second, it does one million stat calls.

        Oh, I see. You are figuring the rename() updates the mtime for the file. It doesn't, not on any unix variant I've used, anyhow...

        So, yes, the loop could of ended up running amock, but it could have run once and dropped out. Depends on the mtime of the file that was ftp'ed in and when the script was run.

        In any case, I added the sleep()...thanks again for the catch.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-18 19:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found