0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ARM機でCPU負荷実験しながら温度監視するCプログラム

Last updated at Posted at 2025-10-05

目的

無限ループでARMのCPUに負担をかけて温度が上がるか実験してみました。

test.c
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <stdbool.h>
#include <sys/sysinfo.h>
#include <sys/resource.h> // nice()用

bool running = true;

// CPU温度を読み取る関数
int read_cpu_temp() {
    FILE *fp;
    int temp;
    fp = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
    if (fp == NULL) return -1;
    fscanf(fp, "%d", &temp);
    fclose(fp);
    return temp;
}

// CPU負荷をかける関数
void *cpu_stress(void *arg) {
    while (running) {
        volatile unsigned long x = 0;
        for (int i = 0; i < 1000000; i++) {
            for (int j = 2; j * j <= i; j++) {
                if (i % j == 0) break;
            }
            x += i;
        }
    }
    return NULL;
}

// 温度監視用関数
void *monitor_temp(void *arg) {
    nice(10); // 監視スレッドの優先度を下げる
    while (running) {
        int temp = read_cpu_temp();
        if (temp != -1) {
            printf("CPU Temp: %.2f°C\n", temp / 1000.0); // 温度表示(英語)
        }
        sleep(10);
    }
    return NULL;
}

int main() {
    int num_cores = get_nprocs(); // CPUコア数
    pthread_t threads[num_cores]; // 計算スレッド用

    printf("CPU Cores: %d\n", num_cores);
    printf("Worker Threads: %d (100%% load)\n", num_cores - 1);
    printf("Monitoring Thread: Low priority\n");
    printf("Press Ctrl+C to stop\n\n");

    // 監視スレッド起動
    pthread_t monitor_thread;
    pthread_create(&monitor_thread, NULL, monitor_temp, NULL);

    // 計算スレッド起動
    for (int i = 0; i < num_cores - 1; i++) {
        pthread_create(&threads[i], NULL, cpu_stress, NULL);
    }

    getchar();
    running = false;

    // スレッドの終了待機
    for (int i = 0; i < num_cores - 1; i++) {
        pthread_join(threads[i], NULL);
    }
    pthread_join(monitor_thread, NULL);

    return 0;
}
testuser@CasaOS:~/test$ nano test.c
testuser@CasaOS:~/test$ gcc test.c -o test -lpthread
testuser@CasaOS:~/test$ ./test
CPU Cores: 4
Worker Threads: 3 (100% load)
Monitoring Thread: Low priority
Press Ctrl+C to stop

CPU Temp: 48.75°C
CPU Temp: 60.31°C
CPU Temp: 61.25°C
CPU Temp: 63.44°C
CPU Temp: 64.38°C
CPU Temp: 63.75°C
CPU Temp: 64.38°C
CPU Temp: 63.44°C
CPU Temp: 64.69°C
CPU Temp: 65.62°C
CPU Temp: 65.00°C
CPU Temp: 65.31°C
CPU Temp: 65.94°C
CPU Temp: 65.94°C
CPU Temp: 65.62°C
CPU Temp: 64.38°C
CPU Temp: 66.56°C
CPU Temp: 65.62°C
CPU Temp: 65.31°C
CPU Temp: 66.56°C
CPU Temp: 65.62°C
CPU Temp: 66.25°C
CPU Temp: 65.94°C
CPU Temp: 67.50°C
CPU Temp: 67.50°C
CPU Temp: 67.81°C
CPU Temp: 67.50°C
CPU Temp: 65.62°C
CPU Temp: 66.56°C
^C

image.png

動作環境(Linux / ARM 版)

  • OS: Armbian 23.08.0-trunk (Debian 12 Bookworm)
  • Kernel: Linux 6.1.38-meson (armv7l)
  • CPU: ARM Cortex-A5, 4 cores, Little Endian
  • GCC: 12.2.0
  • Architecture: armv7l
  • 備考: Windows10からSSH接続して使用

動作環境確認コマンドは以下の通り

uname -a
cat /etc/os-release
gcc --version
lscpu
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?