DEV Community

Chris
Chris

Posted on β€’ Edited on

3 2 2 1 1

How to 100% CPU

I've been working with many sysadmins over the years and one question comes up from time to time: "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) {}}
Enter fullscreen mode Exit fullscreen mode

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 four 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();
}
Enter fullscreen mode Exit fullscreen mode

(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 simplest infinite loop in assembly, it becomes clear the CPU is busy doing something: Executing a jmp instruction that "jumps to itself", as fast as possible. So the CPU is busy running instructions. It's not doing nothing like with a pause/sleep.

global _start

_start:
    jmp _start
Enter fullscreen mode Exit fullscreen mode

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.

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
πŸŽ₯ Audio/video file upload with real-time preview
πŸ—£οΈ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
πŸ“€ Export interview's subtitles in VTT format

Read full post

Top comments (1)

Collapse
 
mayowakalejaiye profile image
mayowa-kalejaiye β€’
Comment hidden by post author

Some comments have been hidden by the post's author - find out more

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay