How to get CPU usage in C?

I want to get the total shared CPU usage for the application in C, the shared CPU usage, as we get in the TaskManager ... I want to know ... for windows and linux :: current The shared CPU usage by all processes ..... as we see in the task manager.

+5
source share
4 answers

It depends on the platform:

  • On Windows, you can use the function GetProcessTimes().
  • On Linux, you can just use clock().

They can be used to measure the amount of processor time taken between two time intervals.

EDIT:

( ), # , , :

% CPU usage = (CPU time) / (# of cores) / (wall time)

# :

+10

POSIX getrusage (2) ru_utime. RUSAGE_SELF RUSAGE_CHILDEN (2) ed. Linux RUSAGE_THREAD . ru_stime, , ru_utime ( ).

+1

.

You can use the clock function by returning clock_t(some integer type, for example, long). On Linux systems, it measures the processor time in microseconds.

+1
source

This is what I was looking for. By manipulating them on my way, I launch it successfully.

Get overall CPU usage

-1
source

All Articles