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


in reply to Temporary usage of RAM memory

Rather than storing your count in a file, I'd store it in a file's name.

Create a directory, and create a single file in that directory who's name is the count. Provided the directory is local, rename is atomic without additional locking.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Temporary usage of RAM memory
by davido (Cardinal) on Apr 11, 2014 at 00:46 UTC

    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

      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

      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.

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

    To: BrowserUk

    Thank you for your time and effort, replying to my question. Sounds interesting I was not aware of this capability of Perl. I will try to implement a small example and see if it works based on my needs. In any case it is nice to know possible alternative solutions for future work.

    Again, thank you for your time.