http://qs321.pair.com?node_id=1081896


in reply to Re: Temporary usage of RAM memory
in thread Temporary usage of RAM memory

rename is atomic, but readdir followed by rename isn't. So if the goal is to increment a number encoded into a filename, one has to read the directory to find the file's number, and then rename it. If there's the possibility of two or more invocations of the script working in close time-proximity there's a race condition, and I don't see how the rename approach can eliminate the need for some type of flocking.

It's possible you've thought this through, and I'm missing something simple. If so, I'm open to learning the new trick. ;)

(Or maybe I'm reading too much into your suggestion.)


Dave

Replies are listed 'Best First'.
Re^3: Temporary usage of RAM memory
by fullermd (Priest) on Apr 11, 2014 at 05:16 UTC

    Only one of the racing processes can win the rename, so you have to check it for failure and re-try the readdir/rename loop. So you don't need locking, but you do need checking and retrying.

      Thanks, that makes sense. :)


      Dave

Re^3: Temporary usage of RAM memory
by thanos1983 (Parson) on Apr 11, 2014 at 08:47 UTC

    To: Dave

    Thank you for your time and effort, I was not even aware of all these capabilities of perl. I am new into programming so I only know the basics. It is interesting to know and apply new ideas.

    Again thank you for your time and effort, It is nice to learn something new everyday.