How to 100% CPU

I’ve been working with many sysadmins over the years and one question comes up at least twice a year: “I quickly need to create some dummy CPU load on this machine, what cpu stress tool should I install?”

If our need is very basic (i.e. we just want t…


This content originally appeared on DEV Community and was authored by Chris

I've been working with many sysadmins over the years and one question comes up at least twice a year: "I quickly need to create some dummy CPU load on this machine, what cpu stress tool should I install?"

If our need is very basic (i.e. we just want to see 100% CPU load on one or multiple cores), maybe we should consider building our own.

The One-Liner

All we need is to put this line of C code in a file, build it with gcc -o stressme stressme.c (or on Windows cl stressme.c) and run it with ./stressme (or stressme.exe).

int main() {while (1) {}}

And while the program runs, we'll see 100% CPU load on one core. For multiple cores, we could start the program multiple times.

Multi-Threaded

Or we could use threads, here's a variant that uses 4 POSIX threads:

#include <pthread.h>
#include <unistd.h>

#define NUM_THREADS 4

void *loop(void *arg) {
    while (1) {}
}

int main() {
    pthread_t threads[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; i++)
        pthread_create(&threads[i], 0, loop, 0);
    pause();
}

(To build it, add the -pthread flag: gcc -o multistress multistress.c -pthread)

Why does this work?

We're running an infinite loop. When we look at the code of the C one-liner in assembly, it becomes clear the CPU is busy doing only one thing: Executing a jmp instruction that "jumps to itself", as fast as possible.

global _start

_start:
    jmp _start

If we were on an older operating system with a cooperative multitasking scheduler, such an infinite loop would probably make our system unresponsive. On today's preemptive multitasking systems, infinite loops cause the program to consume all available processor time, but can still be terminated.


This content originally appeared on DEV Community and was authored by Chris


Print Share Comment Cite Upload Translate Updates
APA

Chris | Sciencx (2025-01-08T20:41:58+00:00) How to 100% CPU. Retrieved from https://www.scien.cx/2025/01/08/how-to-100-cpu/

MLA
" » How to 100% CPU." Chris | Sciencx - Wednesday January 8, 2025, https://www.scien.cx/2025/01/08/how-to-100-cpu/
HARVARD
Chris | Sciencx Wednesday January 8, 2025 » How to 100% CPU., viewed ,<https://www.scien.cx/2025/01/08/how-to-100-cpu/>
VANCOUVER
Chris | Sciencx - » How to 100% CPU. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/08/how-to-100-cpu/
CHICAGO
" » How to 100% CPU." Chris | Sciencx - Accessed . https://www.scien.cx/2025/01/08/how-to-100-cpu/
IEEE
" » How to 100% CPU." Chris | Sciencx [Online]. Available: https://www.scien.cx/2025/01/08/how-to-100-cpu/. [Accessed: ]
rf:citation
» How to 100% CPU | Chris | Sciencx | https://www.scien.cx/2025/01/08/how-to-100-cpu/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.