You can't. You either have 100% of CPU time, or 0%, assuming one processor. That is, the CPU is either running your program, or it isn't.

However, over a given time period, you can have had N%, 0 <= N <= 100, of the available CPU time. To archieve that, just before finishing the program, measure how much CPU time the program has had, how much time has passed, and calculate how long you have to sleep before doing the exit. But remember, you cannot sleep negative time. Instead of sleeping, you may have to doing a lot of work to increase the average CPU time (if the machine is busy, this may require the process get root permission and kill other processes, and/or decrease its niceness, so it can get more or longer time slots).

Calculations can get more hairy if you have more than 1 processor, and more threads. And ambiguous. Does 100% CPU time mean using the full power of all CPUs, or just the equivalent of one?


In reply to Re: how to occupy the processor for a fixed percentage of it by JavaFan
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.