GET Process Cpu Use in c

How can I get the process of using a processor in c ??

I need to use evrey processor and threads in cpu.

please give me an example.

Thanks!

+1
source share
2 answers

In simple C, this is not possible, but since the question is also marked as "Windows":

CPU usage is processor time divided by real-time time. The GetThreadTimes and GetProcessTimes functions provide you with this information (among other things, such as the performance counters mentioned by Joachim Pieleborg, but I think this is probably easier).

You probably also want to use CreateToolhelp32Snapshot to find out which processes and threads exist at all. You will need to translate the thread / process identifiers into descriptors, but I think this will not be a big obstacle (i.e. OpenProcess ).

+2
source

In C, overall CPU usage can be determined using Performance Counters (there is a small typo in the sample code: sleep should be changed to sleep ).

In C ++, C #, Delphi, etc. I would recommend using WMI .

== EDIT ==

I found an approach to use a processor for each process . For example, to get the Microsoft Outlook processor load, change the counter path in the above example to the following:

 PdhAddCounter(query, TEXT("\\Process(OUTLOOK)\\% Processor Time"), 0, &counter); 

If you have multiple instances of the same executable, you can use indexes . This MSDN example is also very useful.

+2
source

Source: https://habr.com/ru/post/1500979/


All Articles