简体   繁体   中英

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? Bad memory allocation? I compiled it with 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. It takes every available CPU cycle, because you've requested it to do so.

A couple things:

The CPU running at 100% is not too uncommon. 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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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