1

I'm writing a process explorer project.
I can get some Information about processes using Win32 Tool Help Snapshot.
but I can't calculate the amount of CPU usage of each process.
It's a C Window Console Application.

The Pianist
  • 546
  • 2
  • 13
  • 23
  • 5
    It is available as a performance counter, Process + % Processor Time. The api is documented here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa373083%28v=vs.85%29.aspx Looks to me you are actually using advice given to you in previous questions, you should accept such an answer. – Hans Passant May 16 '12 at 11:48
  • http://stackoverflow.com/questions/1364801/calculate-cpu-usage-for-a-process , http://stackoverflow.com/questions/4109345/how-to-gauge-cpu-usage-without-taskmgr , http://stackoverflow.com/questions/6664257/monitor-cpu-and-memory-consumption-of-a-specific-processes-in-c-windows –  May 25 '12 at 18:28
  • its duplicate of the following question : http://stackoverflow.com/q/1420426/143897 – Jay D May 25 '12 at 19:33

1 Answers1

0

Maybe the ISO C standardized clock() function does what you need.

$ man clock

NAME clock — determine processor time used

LIBRARY Standard C Library (libc, -lc)

SYNOPSIS #include < time.h>

 clock_t
 clock(void);

DESCRIPTION The clock() function determines the amount of processor time used since the invocation of the calling process, measured in CLOCKS_PER_SECs of a second.

RETURN VALUES The clock() function returns the amount of time used unless an error occurs, in which case the return value is -1.

You would call it once at the start of main(), save the result and later on compute (clock() - result)/CLOCKS_PER_SEC (beware of rounding for integral division!).

Jens
  • 69,818
  • 15
  • 125
  • 179
  • Maybe the ISO C standardized `clock()` function does what you need. > DESCRIPTION > The clock() function determines the amount of processor time used since > the invocation of the calling process, measured in CLOCKS_PER_SECs of a > second. > This is per application. If you want global, see /proc/loadavg (under linux). Nothing related to this question. – elcuco May 28 '12 at 05:52