Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Temporary usage of RAM memory

by thanos1983 (Parson)
on Apr 10, 2014 at 21:09 UTC ( #1081871=perlquestion: print w/replies, xml ) Need Help??

thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have implemented a short sample code of question for better explanation.

I am trying to use a string storing an integer that I want to increment when ever I would be calling my sub program.

My solution so far is storing the value on a .txt file reading the value and icrementing it on every round. Sample of working code is provided under.

I am wondering if there is a possible solution, to store the value to an allocated memory, that will be not used until I calle it again. I want my subprogram to be able to complete the process and not loose the value from the memory. I was even thinking in worst case scenario to use MySQL database but I want to avoid this solution.

#!/usr/bin/perl use strict; use warnings; my @words; my $word; my $text; my $file = "test.txt"; open (DATA, "+<", $file) or die("Could not open file: ".$file.". $!\n"); sub add { print "I am in\n"; while( <DATA> ) { chomp; @words = split(''); foreach $word (@words) { print "This is word: ".$word."\n"; $text = $word + 1; print "This is the new word: ".$text."\n"; } } } &add(); print DATA "".$text."\n"; close (DATA);

Thank you for your time and effort.

Replies are listed 'Best First'.
Re: Temporary usage of RAM memory
by BrowserUk (Pope) on Apr 10, 2014 at 22:45 UTC

    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.

      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.

        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.

      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.

Re: Temporary usage of RAM memory
by Anonymous Monk on Apr 10, 2014 at 21:35 UTC

    No, memory doesn't work like that.

    The computer program that is called operating system generally restricts allocating memory (often RAM) only to running processes

    So to persist RAM allocation past the life of your program, you need another constantly running program , a daemon / service, one specifically designed to persist RAM allocations ... like shared memory

    And you need a special interface to communicate with said memory keepalive program, like IPC::ShareLite, Apache::SharedMem, IPC::Shareable

    You don't need to use mysql, you can use a regular file with proper flocking

      To: Anonymous Monk

      Thank you for the reply and the analysis on my question. I was not even aware of this capabilities. I will take a look and propably I will apply this solution to my problem. ;)

      Thank you for your time and effort.

Re: Temporary usage of RAM memory
by zentara (Archbishop) on Apr 11, 2014 at 10:12 UTC
    If you are on linux, the simplest way would be to create what is called a ramdisk. That is where you create a filesystem in a separate section of memory, usually sized in 4k chunks. It would have the same effect as temporary usage of RAM.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      To: zentara,

      I am sorry, for the late reply I just saw the message. I will take a look a bit further into it. Thank you for your time and effort.

      Best Regards,

      Thanos

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081871]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2020-12-01 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?