This is impossible to do for anything other than a completely quiescent individual system.

Let's say you write a program that alternates between a tight loop and a short sleep and you adjust the timings so that when run it consumes exactly 70% cpu. What happens when you run a second copy concurrently? (Or play an MP3 or stream a movie; or check your email)

Another approach might be to query the current cpu usage and then increase or decrease your loop constant to compensate. But the figure you queried was for the previous time period. So now both copies of your program discover that the usage was 90%, so they both step back. But now they get 50% so they ramp up. And now they hit 100% so they step back again. This problem is called hysteresis. And if you succeed in getting 2 copies to balance over some time period, start a third copy (or play an mp3 ... etc. )

Now, assuming your purpose for this is to test the responsiveness of some other program when the system is under load. Perform your test when you are consuming 100% cpu. Adjust the priorities of your tasks (cpu bound lower; IO responsive higher), until you meet the needed response time needs of the IO process. If it will respond in a timely fashion when a (correctly prioritized) cpu-bound application is running flat out, it will definitely continue to do so when the system is under less load.


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.
RIP an inspiration; A true Folk's Guy

In reply to Re: how to occupy the processor for a fixed percentage of it by BrowserUk
in thread how to occupy the processor for a fixed percentage of it by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.