2

Trying to create a C program to stress test all CPU cores to 100% utilization. However, it works too well such that I have to reboot Windows 11 because everything is unresponsive, even Ctrl + Alt + Delete.

Ctrl+Alt+Delete is a Windows function, baked into the OS. So no matter how frozen a program may be as long as the OS is still working it will detect that key combo and override everything else.

Why doesn't the Windows 11 OS detect Ctrl + Alt + Delete and allow the stress test program to be terminated?

#include <windows.h>
#include <stdio.h>

DWORD WINAPI consume_cpu(LPVOID arg)
{
    while (1) {}
    return 0;
}

int main(int argc, char** argv)
{
    // Get the number of CPU cores
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    int num_cores = sysinfo.dwNumberOfProcessors;

    HANDLE* threads = (HANDLE*)malloc(num_cores * sizeof(HANDLE));

    // Create one thread per CPU core
    for (int i = 0; i < num_cores; i++)
    {
        threads[i] = CreateThread(NULL, 0, consume_cpu, NULL, 0, NULL);
        SetThreadPriority(threads[i], THREAD_PRIORITY_TIME_CRITICAL);
    }

    // Wait for the threads to finish
    WaitForMultipleObjects(num_cores, threads, TRUE, INFINITE);

    free(threads);
    return 0;
}

Notes

As suggested in the comments, removing THREAD_PRIORITY_TIME_CRITICAL allows for 100% CPU utilization plus the OS is now responsive.

cpu100%

Timothy Baldwin
  • 3,551
  • 1
  • 14
  • 23
vengy
  • 1,548
  • 10
  • 18
  • 8
    I don't use windows, but `THREAD_PRIORITY_TIME_CRITICAL` sure sounds like it'd preempt pretty much anything. – Stephen Newell Jan 07 '23 at 01:58
  • 7
    From the documentation of [`SetThreadPriority`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreadpriority): "When manipulating priorities, be very careful to ensure that a high-priority thread does not consume all of the available CPU time. A thread with a base priority level above 11 interferes with the normal operation of the operating system." Note that `THREAD_PRIORITY_TIME_CRITICAL` corresponds to priority level 15. – Andreas Wenzel Jan 07 '23 at 02:01
  • 4
    If you just use the normal prio, you'll still load the CPU to the max - but some of the load will come from the OS trying to ... be an OS. – Ted Lyngmo Jan 07 '23 at 02:03
  • 2
    @Ted Lyngmo Good to know that letting the OS manage the thread priority will still load the CPU to the max. I was trying to hog all cores to 100% (single program) by not allowing the OS to steal any time slices. Thanks. – vengy Jan 07 '23 at 02:11
  • @vengy Unrelated: I made a program called "heater" back in the day when I liked to assemble my own computers. The program was meant to be used to optimize the cooling. I did _something_ right because it made every component with a thermostat a few degrees hotter than other "burn-in" programs :-) No prio-raise needed. – Ted Lyngmo Jan 07 '23 at 02:17
  • You succeeded in your quest too well. You DDOS'ed yourself ;-) I wrote a similar test once. I had to modify it to self terminate after a specified period of time to prevent a similar system lockup. – Craig Estey Jan 07 '23 at 02:19
  • Inline assembly has nothing to do with this. You're writing in C, `while(1){}` would compile to the same loop you wrote with inline asm. (Unlike C++, it's well-defined to write an infinite loop without any `volatile` side-effects or I/O in C.). – Peter Cordes Jan 07 '23 at 03:36
  • 1
    @TedLyngmo: These days, the max power workload on modern x86 CPUs involves keeping both SIMD FMA pipelines full. Those mantissa multipliers create the most amount of transistors switching every clock cycle. Prime95 stress test comes pretty close to doing that, and also vector loads. Similar to [How do I achieve the theoretical maximum of 4 FLOPs per cycle?](https://stackoverflow.com/q/8389648) for older CPUs without AVX+FMA. That uses *much* more power than just executing one taken `jmp` per clock cycle like the code in this question. – Peter Cordes Jan 07 '23 at 03:55

0 Answers0