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

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.

  • Comment on Re: Re: When is it safe to move a file?

Replies are listed 'Best First'.
Re: When is it safe to move a file?
by Dominus (Parson) on Jan 15, 2001 at 04:01 UTC
    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.

        Says kschwab:
        Oh, I see. You are figuring the rename() updates the mtime for the file.
        No, I'm not. Here is what your loop looked like before you added the sleep at my suggestion:

        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); }

        Suppose your program executes the rename and then enters this loop just after the remote process has finished updating the file. In that case, the file's mtime is the current time. You then spend five minutes busy-waiting in this loop.

        Or instead, suppose that the remote process reopens the file for writing just before you do the rename, and spends sixty minutes updating the file. Your program will spend 65 minutes busy-waiting in the loop.