How do I get the CPU occupied by each process in real time? - Discussion and ask

zhaozj2021-02-16  51

The purpose of this paper is to discuss a problem with you, and give a function of the CPU usage based on the process ID to return this process. I hope everyone will not forget my problem when using this function.

There are two main methods for calculating the process CPU usage. One is calculated according to time, and the other is calculated according to the registry, I am using the former. Principle Method: Calculate the total time of a particular process and the total time of all processes in a specific period of time, and their ratio is the specific process.

I wrote a function (rewritten), as a parameter, the process CPU usage as the return value, calculate the CPU occupied by the specific process in real time, and test correctly. The entire program is as follows:

Structure definition: typedef struct _THREAD_INFO {LARGE_INTEGER CreateTime; DWORD dwUnknown1; DWORD dwStartAddress; DWORD StartEIP; DWORD dwOwnerPID; DWORD dwThreadId; DWORD dwCurrentPriority; DWORD dwBasePriority; DWORD dwContextSwitches; DWORD Unknown; DWORD WaitReason;

ThreadInfo, * pthreadinfo;

Typedef struct _unicode_string {ushort length; ushort maxgength; pwstr buffer;} unicode_string;

typedef struct _PROCESS_INFO {DWORD dwOffset; DWORD dwThreadsCount; DWORD dwUnused1 [6]; LARGE_INTEGER CreateTime; LARGE_INTEGER UserTime; LARGE_INTEGER KernelTime; UNICODE_STRING ProcessName;

DWORD DWPROCESSID; DWORD DWPARETPROCESSID; DWORD DWHANDECOUNT; DWORD DWUNUSED3 [2];

DWORD dwVirtualBytesPeak; DWORD dwVirtualBytes; ULONG dwPageFaults; DWORD dwWorkingSetPeak; DWORD dwWorkingSet; DWORD dwQuotaPeakPagedPoolUsage; DWORD dwQuotaPagedPoolUsage; DWORD dwQuotaPeakNonPagedPoolUsage; DWORD dwQuotaNonPagedPoolUsage; DWORD dwPageFileUsage; DWORD dwPageFileUsagePeak;

DWORD DCOMMITCHARGE; ThreadInfo ThreadsysInfo [1];

PROCESSINFO, * PPROCESSINFO;

Function definition:

INT button2click (int ID); // parameter is the process ID

Physical function: int Button2Click (int id) {int cpuusage; PVOID pProcInfo = NULL; DWORD dwInfoSize = 0x20000; PPROCESSINFO pProcessInfo; DWORD dwWorkingSet; long (__stdcall * NtQuerySystemInformation) (DWORD, PVOID, DWORD, DWORD);

Static __int64 lasttotalprocesscpuusage = 0; static __int64 lastcurrentprocesscpuusage = 0;

Int currentdelta; int totaldelta; __ int64 TotalProcessCPUUSAGE = 0; __ int64 currentprocesscpuusage = 0;

/

PPROCICINFO = (pvoid) (New Byte [dwinfosize]);

NTQuerySystemInformation = (long (__ stdcall *) (DWORD, PVOID, DWORD, DWORD) GetProcadDress ("NTDLL.DLL"), "NTQuerySystemInformation");

NTQuerySystemInformation (5, PProcinfo, Dwinfosize, 0);

PPROCESSINFO = (pprocessinfo) PPROCICINFO;

Do {TotalProcessCPUUSAGE = (__INT64) PPROCESSINFO-> kernelTime.quadpart (__int64) PProcessInfo-> useertime.quadpart;

if (pProcessInfo-> dwProcessID == id) {dwWorkingSet = pProcessInfo-> dwWorkingSet; CurrentProcessCPUUsage = (__int64) pProcessInfo-> KernelTime.QuadPart (__int64) pProcessInfo-> UserTime.QuadPart;}

/ IF (pprocessinfo-> dwoffset == 0) {Break;}

PPROCESSINFO = (PPRocessInfo) (Byte *) PPRocessInfo PPRocessInfo-> dwoffset;} While (TRUE);

TotalDelta = TotalProcesscpuusage - LasttotalProcessCPUusage; CurrentDelta = CURRENTPROCPUUSAGE - LastCurrentProcessCPUusage;

IF (TotalDelta! = 0) CPUUSAGE = 100 * CurrentDelta / TotalDelta;

// this-> caption = "CPU =" INTTOSTR (100 * currentdelta / totaldelta) // "Memory =" INTOSTR (DWworkingSet / 1024) "K";

LasttotalProcessCPUusage; LastCurrentProcessCPUusage = CURRENTPROCPUUSAGE = CURRENTPROCPUUSAGE

DELETE [] PPROCICINFO; RETURN CPUUSAGE;

}

Console call: int main (void) {while (true) {int s = button2click (0); / / This process ID is incorporated into Printf ("% D / N", S); SLEEP (1000) } Return 0;} /// The above code is very normal ////

But how do I ask for the CPU usage of each process running in the system, and the list is displayed? ?

I traverse the system process (for loop), then pass each process as a parameter, external timer control, but the value I get is wrong, why? ? ?

Who can get this problem? ?

Question: Use this function to compare the CPU usage of all systems in the system in real time, and display it in the list? ? Thank you, I am using it! ! !

转载请注明原文地址:https://www.9cbs.com/read-21783.html

New Post(0)