[小工具] Command-line CPU Killer(附源碼及下載連接)

博主有次在拆卸本身的筆記本電腦後,發現電腦若是靜置時間長了有時會重啓,但奇怪的是當我本身在電腦前工做的時候歷來沒有重啓過。據此推測可能 CPU 徹底空閒的時候風扇徹底停轉了,雖然 CPU 溫度不高,可是其它芯片仍是會發熱,拆卸的時候導熱硅膠墊沒有裝好,致使其它芯片過熱引起系統重置。windows

解決的辦法也很簡單,就是模擬 CPU 工做的狀態,人爲添加少許負載便可。工具

目前市面上也有個比較流行的工具,叫 CPU Killer,惋惜還要破解了才能用。一個這麼簡單的工具都要破解後才能無限制使用實在是不爽,因此博主準備本身開發一個。spa

代碼實在很簡單,應該沒有什麼講解的必要了,這裏直接貼出源碼:blog

Killer.h圖片

#ifndef KILLER_H
#define KILLER_H

void Start(int cores, double load);
void Stop();

#endif

Killer.cpp開發

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

// Private ////////////////////////////////////////////////////////////////////

static volatile bool running = false;

static void Tick(double usage) // One tick is 1000 ms
{
    unsigned int busyTime = (int)(1000 * usage);
    unsigned int idleTime = (int)(1000 * (1 - usage));

    // Busy
    DWORD t0 = GetTickCount();
    while (GetTickCount() - t0 < busyTime)
        ;

    // Idle
    Sleep(idleTime);
}

struct ThreadContext
{
    int index; // CPU Index (0, 1, 2, ...)
    double load;
};

static DWORD WINAPI WorkerThread(LPVOID lpParam)
{
    ThreadContext *context = (ThreadContext *)lpParam;
    SetThreadAffinityMask(GetCurrentThread(), 1 << context->index);

    while (running)
    {
        Tick(context->load);
    }

    return 0;
}

// API ////////////////////////////////////////////////////////////////////////

void Start(int cores, double load)
{
    running = true;

    for (int i = 0; i < cores; i++)
    {
        ThreadContext *context = new ThreadContext();
        context->index = i;
        context->load = load;

        printf("Creating thread %d / %d ...\n", i + 1, cores);

        CreateThread(0, 0, WorkerThread, context, 0, 0);
    }

    printf("Press q to exit\n");
}

void Stop()
{
    running = false;
    Sleep(1100);
}

Main.cppget

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "killer.h"

int GetNumberOfCores()
{
    SYSTEM_INFO si;
    GetSystemInfo(&si);
    return (int)si.dwNumberOfProcessors;
}

void PressAnyKeyToContinues()
{
    printf("\n");
    printf("Press any key to continue\n");
    _getch();
    exit(0);
}

void main(int argc, char *argv[])
{
    if (argc != 3)
    {
        printf("Command-line CPU Killer v0.1\n");
        printf("---------------------------------------\n");
        printf("Usage: CPU-Killer {Cores} {Target Load}\n");
        printf("Example: CPU-Killer 1 50\n");
        
        PressAnyKeyToContinues();
    }

    int cores = 0;
    int load = 0;
    int totalCores = GetNumberOfCores();

    sscanf_s(argv[1], "%d", &cores);
    sscanf_s(argv[2], "%d", &load);

    if (cores <= 0 || cores > totalCores)
    {
        printf("Invalid number of cores!\n");

        if (totalCores == 1)
            printf("There should be exactly one core.\n");
        else
            printf("There should be %d to %d cores.\n", 1, totalCores);

        PressAnyKeyToContinues();
    }

    if (load < 1 || load > 100)
    {
        printf("Invalid target load!\n");
        printf("The target load should be between 1%% and 100%%.\n");

        PressAnyKeyToContinues();
    }

    printf("Number of CPU cores: %d\n", cores);
    printf("Target load: %d%%\n", load);

    Start(cores, load / 100.0);

    while (true)
    {
        int ch = _getch();
        if (ch == 'q')
            break;
    }

    printf("Exiting...\n");
    Stop();
}

使用方法源碼

鼠標右鍵拖放 CPU-Killer.exe 創建一個快捷方式,而後在後面加上參數便可。參數一共有兩個:it

  • Cores: 須要佔用的 CPU 核數(通常取 1 - 4)
  • Target Load:目標負載(1 - 100)

好比 CPU-Killer.exe 1 50,就是佔用一個核,該核的 CPU 佔用率約爲 50%。io

注意:程序須要 Visual C++ 2008 運行庫!

下載連接

百度網盤:http://pan.baidu.com/s/1o6FUvqy

或者能夠試一下這個圖片:

將圖片保存到本地,而後把擴展名改成 .rar 再解壓便可。

相關文章
相關標籤/搜索