需要一个能让CPU 100%运行的程序

发布于 2024-10-26 06:23:31 字数 118 浏览 5 评论 0原文

我需要一个程序,可以让我的 CPU 100% 运行。

最好是用 C 语言编写一个小程序,它将使 CPU 以 100% 的速度运行,并且一个程序没有被编译器“优化”,因此它什么也不做。

建议?

I need a program, that will make my CPU run at 100%.

Preferably in C, a tiny program, that will make the CPU run at 100%, and one, that is not "optimized" by the compiler, so it does nothing.

Suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

谈场末日恋爱 2024-11-02 06:23:31
int main(void) {
  volatile unsigned x=0, y=1;
  while (x++ || y++);
  return 0;
}

或者,如果您有一个多核处理器 - 未经测试......就像上面的一样:)

int main(void) {
#pragma omp parallel
  {
    volatile unsigned x=0, y=1;
    while (x++ || y++);
  }
  return 0;
}
int main(void) {
  volatile unsigned x=0, y=1;
  while (x++ || y++);
  return 0;
}

Or, if you have a multi-core processor -- untested ... just like the one above :)

int main(void) {
#pragma omp parallel
  {
    volatile unsigned x=0, y=1;
    while (x++ || y++);
  }
  return 0;
}
瞎闹 2024-11-02 06:23:31

这个呢:

int main() { for (;;); }

What about this one:

int main() { for (;;); }
满栀 2024-11-02 06:23:31

这是一个很好的老式叉子炸弹。

#include <unistd.h>
int main(void)
{
  while(1)
    fork();
  return 0;
}

Here's a good, old-fashioned fork bomb.

#include <unistd.h>
int main(void)
{
  while(1)
    fork();
  return 0;
}
情何以堪。 2024-11-02 06:23:31

将其复制并粘贴到名为 source.c 的文件中:

int main(int argc, char *argv) {
    while(1);
}

编译 source.cgcc source.c -o myprogram

运行它: ./我的程序

Copy and paste this in a file named source.c:

int main(int argc, char *argv) {
    while(1);
}

Compile source.c: gcc source.c -o myprogram

Run it: ./myprogram

倒带 2024-11-02 06:23:31

建议空循环的答案只能使双核 CPU 达到 50%,四核 CPU 达到 25% 等。

因此,如果这是一个问题,可以使用类似的东西

void main(void)
{
    omp_set_dynamic(0);
    // In most implemetations omp_get_num_procs() return #cores 
    omp_set_num_threads(omp_get_num_procs());
    #pragma omp parallel for
    for(;;) {}
}

The answers suggesting an empty loop shall only bring dual core CPU to 50%, quad-core to 25% etc.

So if that is an issue one can use something like

void main(void)
{
    omp_set_dynamic(0);
    // In most implemetations omp_get_num_procs() return #cores 
    omp_set_num_threads(omp_get_num_procs());
    #pragma omp parallel for
    for(;;) {}
}
夕色琉璃 2024-11-02 06:23:31

适用于多线程系统的本机 Windows 解决方案。在 Visual C++(或 Visual Studio)上编译,无需任何库。

/* Use 100% CPU on multithreaded Windows systems */

#include <Windows.h>
#include <stdio.h>
#define NUM_THREADS 4

DWORD WINAPI mythread(__in LPVOID lpParameter)
{
    printf("Thread inside %d \n", GetCurrentThreadId());
    volatile unsigned x = 0, y = 1;
    while (x++ || y++);
    return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE handles[NUM_THREADS];
    DWORD mythreadid[NUM_THREADS];
    int i;

    for (i = 0; i < NUM_THREADS; i++)
    {
        handles[i] = CreateThread(0, 0, mythread, 0, 0, &mythreadid[i]);
        printf("Thread after %d \n", mythreadid[i]);
    }

    getchar();
    return 0;
}

Native Windows solution for multithreaded systems. Compiles on Visual C++ (or Visual Studio) without any library.

/* Use 100% CPU on multithreaded Windows systems */

#include <Windows.h>
#include <stdio.h>
#define NUM_THREADS 4

DWORD WINAPI mythread(__in LPVOID lpParameter)
{
    printf("Thread inside %d \n", GetCurrentThreadId());
    volatile unsigned x = 0, y = 1;
    while (x++ || y++);
    return 0;
}


int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE handles[NUM_THREADS];
    DWORD mythreadid[NUM_THREADS];
    int i;

    for (i = 0; i < NUM_THREADS; i++)
    {
        handles[i] = CreateThread(0, 0, mythread, 0, 0, &mythreadid[i]);
        printf("Thread after %d \n", mythreadid[i]);
    }

    getchar();
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文