I am running some test on Amazon EC2 instances and we want to make the CPU always busy at above 80%.

What I have is a program main that needs to run in high priority and I want to launch another program, preferably some math C code or a bash script that loads the CPU to over 80%.

What programs are there to use for such a task, and how to make my program run with the highest priority.

PS: Running Fedora.


Occupying one CPU at 100% (minus overhead) is easy in the shell:

while true; do :; done

If you want to reduce the load, introduce sleeps.

i=0; while [ $i -ne 0 ] || sleep 0.001; do i=$(( (i+1) % 10000 )); done

Tune 10000 up or down to get the desired load.

The scheduling priority is set by nice. You'll need to be root to set a higher-than-default priority. Note that a negative niceness means high-priority (a positive niceness means be nice, i.e. low priority).

nice -20 sh -c 'while …'

With "nice" you can control the priority. For the highest priority (only available for root):

nice -n -20 yourprogram

And for the lowest:

nice -n 19 yourprogram

If you need control also the IO, use ionice. "man nice" and "man ionice" for the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.