zoukankan      html  css  js  c++  java
  • [小工具] Command-line CPU Killer(附源码及下载链接)

    博主有次在拆卸自己的笔记本电脑后,发现电脑如果静置时间长了有时会重启,但奇怪的是当我自己在电脑前工作的时候从来没有重启过。据此推测可能 CPU 完全空闲的时候风扇完全停转了,虽然 CPU 温度不高,但是其它芯片还是会发热,拆卸的时候导热硅胶垫没有装好,导致其它芯片过热引发系统重置。

    解决的办法也很简单,就是模拟 CPU 工作的状态,人为添加少量负载即可。

    目前市面上也有个比较流行的工具,叫 CPU Killer,可惜还要破解了才能用。一个这么简单的工具都要破解后才能无限制使用实在是不爽,所以博主准备自己开发一个。

    代码实在很简单,应该没有什么讲解的必要了,这里直接贴出源码:

    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 ...
    ", i + 1, cores);
    
            CreateThread(0, 0, WorkerThread, context, 0, 0);
        }
    
        printf("Press q to exit
    ");
    }
    
    void Stop()
    {
        running = false;
        Sleep(1100);
    }
    

    Main.cpp

    #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("
    ");
        printf("Press any key to continue
    ");
        _getch();
        exit(0);
    }
    
    void main(int argc, char *argv[])
    {
        if (argc != 3)
        {
            printf("Command-line CPU Killer v0.1
    ");
            printf("---------------------------------------
    ");
            printf("Usage: CPU-Killer {Cores} {Target Load}
    ");
            printf("Example: CPU-Killer 1 50
    ");
            
            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!
    ");
    
            if (totalCores == 1)
                printf("There should be exactly one core.
    ");
            else
                printf("There should be %d to %d cores.
    ", 1, totalCores);
    
            PressAnyKeyToContinues();
        }
    
        if (load < 1 || load > 100)
        {
            printf("Invalid target load!
    ");
            printf("The target load should be between 1%% and 100%%.
    ");
    
            PressAnyKeyToContinues();
        }
    
        printf("Number of CPU cores: %d
    ", cores);
        printf("Target load: %d%%
    ", load);
    
        Start(cores, load / 100.0);
    
        while (true)
        {
            int ch = _getch();
            if (ch == 'q')
                break;
        }
    
        printf("Exiting...
    ");
        Stop();
    }
    

    使用方法

    鼠标右键拖放 CPU-Killer.exe 建立一个快捷方式,然后在后面加上参数即可。参数一共有两个:

    • Cores: 需要占用的 CPU 核数(一般取 1 - 4)
    • Target Load:目标负载(1 - 100)

    比如 CPU-Killer.exe 1 50,就是占用一个核,该核的 CPU 占用率约为 50%。

    注意:程序需要 Visual C++ 2008 运行库!

    下载链接

    百度网盘:http://pan.baidu.com/s/1o6FUvqy

    或者可以试一下这个图片:

    将图片保存到本地,然后把扩展名改为 .rar 再解压即可。

  • 相关阅读:
    delphi不同版本字符串类型的演化(要支持基于firemonkey的app调用,字符串最好使用olevariant类型)
    IdHttpServer实现webservice(130篇DataSnap文章)
    hdu 1809 求SG函数
    delphi中无类型文件读写
    delpi中的RTTI初试
    后台调用外部程序的完美实现(使用CreateDesktop建立隐藏桌面)
    delphi之完美Splash方案(在TfrmMain.FormCreate里不断调用TfrmSplash显示加载进度文字,并且及时Update显示)
    查看内存数据的函数(ByteToHex和ByteToBin,最终都变成String)
    SQLsever2008 远程连接错误 linq
    delphi 利用HTTP的POST方法做个在线翻译的小工具 good
  • 原文地址:https://www.cnblogs.com/F-32/p/4344342.html
Copyright © 2011-2022 走看看