Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Buffer overflow

by PrakashK (Pilgrim)
on Jul 07, 2001 at 01:53 UTC ( #94628=note: print w/ replies, xml ) Need Help??


in reply to Buffer overflow

You seem to be calling several subroutines in your script. It is difficult to know what they might be doing without the complete code.

Here's my guess: You have a memory leak (perhaps from using global variables), or if you are opening files and not closing them, you will eventually reach the max limit on file descriptors.

Try calling your perl script from a shell script loop:

#!/bin/ksh TIMER=10 while : do perl script.pl sleep $TIMER done
If that is not an option, you can also create another perl script that uses 'do' function to call this script:
my $TIMER = 10; while (1) { do "/path/to/your/script.pl"; sleep $TIMER; }
/prakash


Comment on Re: Buffer overflow
Select or Download Code
Re: Re: Buffer overflow
by mikeo (Initiate) on Jul 07, 2001 at 02:06 UTC
    Thanks for the quick response PrakashK, but I forgot to mention that I had tried your second option. The results of that made me even more confused as to what was going on. If I call the script from the command line, it will work fine. But it gives me the "overflow" problems if I call it from a file like this:
    #!/usr/bin/perl my $TIMER = 10; $TEST = 1; while ($TEST == 1) { do "mrtgnav.pl"; sleep $TIMER; $TEST = 1; }
    Note the $TEST variable. When I run what you see above, it has the problem. But the program works when I try and fool it like this:
    #!/usr/bin/perl my $TIMER = 10; $TEST = 1; while ($TEST == 1) { do "mrtgnav.pl"; sleep $TIMER; $TEST = 0; }
    Help. My head is starting to hurt...

      I expect the reason it give you no problems the second way is that you're only running it once, sleeping for 10 seconds, and exiting (you exit the loop on the first time through).

      I think you and PrakashK are both under a slightly misapprehension as to what do does: it "does" the code in a file you specify, but it does it within the name space of the currently running script (and it does the same thing every time you do it). This is very useful for importing configuration variables, but not very useful for avoiding memory leaks.

      I suspect, therefore, that if you try substituting system for do, it will solve that problem, giving you an acceptable work-around for running the script repeatedly.

      It will not solve your overall problem, however: most likely that is caused by the use of global variables in a non-scalable manner, as indicated by synapse0 and PrakashK. This can be solved by making your variables lexically scoped, using my (this makes it sound slightly simpler than it is). I'd explain why this is, but Dominus explains it so much better in Coping with Scoping (TPJ) that I'll just recommend that you read that instead. :-)

      If you're modifying globals in your subroutines (which it seems very likely you are), don't do that! :-) Instead, pass values to them explicitly, and give them useful return values. If you write subs this way, casual readers like us (and you, two months from now) will be able to figure out which variables they modify, and mysterious bugs will be less likely to happen.

      I strongly suggest using use strict; at the top of every large script you write (and the small ones too, just to keep in practice). Again, I'd explain exactly why (it has to do with those lexically scoped variables), but tachyon wrote a node precisely to avoid that duplication of effort, so I'll just point you to it: Use strict warnings and diagnostics or die. (Though if you're new to Perl I should perhaps note that he doesn't mean that last part entirely literally...)

      Sorry for the somewhat scattershot nature of this reply--sometimes that's all my brain will produce.



      If God had meant us to fly, he would *never* have give us the railroads.
          --Michael Flanders

        Thanks for the great info! That was some really good reading for someone new in the language. Substituting system for do got rid of the memory leak for me. Thanks again for the great post.
        Thanks for clarifying what do does.

        /prakash

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-08-20 09:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (110 votes), past polls