[小工具] 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 ...\n", i + 1, cores);

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

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

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("\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 建立一个快捷方式,然后在后面加上参数即可。参数一共有两个:

  • 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 再解压即可。

转载于:https://www.cnblogs.com/F-32/p/4344342.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值