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

Is it possible to write a script which can use N% of CPU, where N is the argument given to the program....

If yes, kindly give me hint to write it to do so, or give the script to do that.

Replies are listed 'Best First'.
Re: hogging N% of CPU ?
by zentara (Cardinal) on Aug 27, 2010 at 11:40 UTC
    See (OT) Run task only when resources have been free for x time and see the Posix nice usage. Also check out BSD::Resource

    Here is an old hack.

    #!/usr/bin/perl #Description: These subs allow you control how much % CPU maximum wil +l use your script. CPU_start() must be called once when you script st +art. #This example script will use 30% CPU until Ctrl-C pressed: CPU_start(); CPU_max(30) while 1; use Time::HiRes qw(time); sub CPU_used { (map {$_->[13]+$_->[14]} [split " ", Cat("/proc/self/stat")])[0] } { my %start = (tm => 0, cpu => 0); sub CPU_start { @start{"tm","cpu"} = (time(),CPU_used()) } sub CPU_max { my ($max, $real, $cpu) = ($_[0], time()-$start{tm}, CPU_used()-$start{cpu}); return unless defined($max) and $max > 0; &sleep( $cpu/$max-$real ); }} # # macro used from CPU_used() and CPU_max() # sub sleep { select undef,undef,undef,$_[0] } sub Cat { local *F; open F, "< ".$_[0] or return; local $/ unless wantarray; return <F>; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: hogging N% of CPU ?
by JavaFan (Canon) on Aug 27, 2010 at 12:12 UTC
    Not easily. Note that at every moment in time, your process (assuming a single core-single CPU system) takes either 100% of CPU time, or 0%. That is, either your process is running, or it's waiting in the run queue.

    So, "N% of CPU" doesn't even make sense. Is that N% over 1 minute? An hour? A day? The lifetime of the computer?

    So, when you say "N% of CPU", what do you actually mean?

Re: hogging N% of CPU ?
by Marshall (Canon) on Aug 27, 2010 at 13:54 UTC
    That's hard to do and its unclear to me what the rationale would be, with the exception of power saving on a laptop. Search for "windows cpu throttle" and that will give you some ideas.

    The normal thing would be for you to differentiate between what's important and what's not. This is a rough allocation of CPU resources and that's done by priority levels. Yes, even Windows has this idea. Type: "help start" at the command prompt. If you start a program at the IDLE priority level, that means when the CPU has nothing else to do, "run me". Now if the computer isn't really doing anything, then your program might take close to 100% of the CPU!

    If you have something "more important" then you run it at a higher priority level. There are lots of levels, only a few of which are accessible from the command line, but more are accessible via various API's.

    How the OS decides what to do next is of course very OS specific and this whole subject can get VERY complicated. But normally for the average user, saying just "I'm normal and should share with my peers", "I'm less important than other stuff" and "I'm really not that important at all" is enough (these are priority levels).

    Update: Its summer time now and I don't run my CPU at 100% during the summer. But during the winter, especially on the cold nights...I crank this baby up! I donate CPU power to SETI@Home or Einstein@Home or one of the other BOINC projects. This basically turns my computer into a space heater!

    This is an example of a "well behaved" low priority compute intensive task. I don't notice a difference at all in terms of response times, but the room stays warmer! When I'm not doing anything, one of the BOINC projects is maxing out my CPU MIPS. Anyway, instead of running electric space heaters, consider running the heck out of your computer in the interest of science! They've got the priority scheduling part right and it works. The watts that the space heater consumes does nothing useful besides heating the room. The watts that the computer consumes heats the room and at least has the potential for doing something useful.

Re: hogging N% of CPU ?
by talexb (Chancellor) on Aug 27, 2010 at 17:07 UTC

    I've written scripts that look at the load (/proc/loadavg under Linux) and sleep briefly until the load's below the upper limit. You might think about doing that instead of trying to limit yourself to a percentage of the CPU available.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds