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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |