简体   繁体   English

使用100%CPU的C ++编译代码

[英]C++ compiled code using 100% CPU

I have the following code (it gets all processes then search for a regex pattern in them, code for a larger personal project for malware detection), the code does what I want but the only problem it is using 100% of CPU, what do I do wrong? 我有以下代码(获取所有进程,然后在其中搜索正则表达式模式,为较大的个人项目进行代码检测恶意软件),该代码可以实现我想要的,但唯一的问题是使用100%的CPU,该做什么?我做错了吗 Bad memory allocation? 内存分配错误? I compiled it with MS Visual Studio 2010 (cl.exe /EHsc mycode.cpp) 我使用MS Visual Studio 2010(cl.exe / EHsc mycode.cpp)进行了编译

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <tlhelp32.h>
#include <psapi.h>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
#include <regex>

using namespace std;

#pragma comment(lib, "psapi.lib") 

void PrintProcessNameAndID(DWORD);
void find_locs(HANDLE process);
void ListProcesses();

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

void find_locs(HANDLE process) {

    unsigned char *p = NULL;
    MEMORY_BASIC_INFORMATION info;

    for ( p = NULL;
        VirtualQueryEx(process, p, &info, sizeof(info)) == sizeof(info);
        p += info.RegionSize )
    {
        std::string buffer;

        if (info.State == MEM_COMMIT &&
            (info.Type == MEM_MAPPED || info.Type == MEM_PRIVATE))
        {
            DWORD bytes_read;

            buffer.resize(info.RegionSize);
            ReadProcessMemory(process, p, &buffer[0], info.RegionSize, &bytes_read);
            buffer.resize(bytes_read);

            const std::tr1::regex rx("([\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7})");
            std::tr1::match_results<std::string::const_iterator> res; 
            std::tr1::regex_search(buffer, res, rx);

            ofstream myfile;
            myfile.open ("proc.txt", ios::app);

            for (unsigned int i=0; i<res.size(); ++i)
            {
                std::cout << res[i] << std::endl;
                myfile << res[i] << "\n";
            }

            myfile.close();
        }
    }
}

void ListProcesses()
{
    DWORD aProcesses[1024];
    DWORD cbNeeded;
    DWORD cProcesses;
    unsigned int i;

    if (!EnumProcesses(aProcesses,sizeof(aProcesses),&cbNeeded))
        return;

    cProcesses = cbNeeded / sizeof(DWORD);

    for ( i = 0; i < cProcesses; i++ )
    {
        PrintProcessNameAndID(aProcesses[i]);
    }
}

void PrintProcessNameAndID(DWORD processID)
{
    TCHAR szProcessName[MAX_PATH]; // = TEXT("<unknown>");

    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);

    if (NULL != hProcess)
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
             &cbNeeded))
        {
            GetModuleBaseName(hProcess, hMod, szProcessName,
                               sizeof(szProcessName)/sizeof(TCHAR));
        }
    }
    _tprintf(TEXT("pid: %u file: %s\n"), processID, szProcessName);
    find_locs(hProcess);
    CloseHandle(hProcess);
}

Thanks for help! 感谢帮助!

一个程序占用了100%的处理器,这没什么问题((我不知道如何将这个答案扩展到这个范围之外))

Any program running continuosly without a Sleep call (some sort of saying to OS "I'm done for now") will try to run as fast as possible, requesting next iteration of the loop just after the previous one. 连续运行而没有Sleep调用的任何程序(对操作系统“我已经做完了”)都将尝试尽可能快地运行,并在上一个循环之后请求下一次循环迭代。 It takes every available CPU cycle, because you've requested it to do so. 它需要每个可用的CPU周期,因为您已请求这样做。

A couple things: 几件事:

The CPU running at 100% is not too uncommon. 以100%运行的CPU并不少见。 This is especially true if you are running computationally intensive tasks (such as prime number computations). 如果您正在运行计算密集型任务(例如素数计算),则尤其如此。 Eg: 例如:

How to get 100% CPU usage from a C program 如何从C程序获取100%CPU使用率

Or, what is probably more applicable in your case, is that it's due to a myriad combination of things related to windows itself, your hardware combination and configuration: 或者,更可能适用于您的情况是由于与Windows本身,您的硬件组合和配置相关的各种事情的组合:

http://www.techradar.com/us/news/computing/why-is-my-cpu-running-at-100-710254 http://www.techradar.com/us/news/computing/why-is-my-cpu-running-at-100-710254

In all, it's not something to be too worried about. 总而言之,不必太担心。 Generally , that is. 一般而言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM