Problems in getting Process Time in C ( Please help me )

老子叫甜甜 提交于 2019-12-13 07:54:33

问题


I am a newbie in C and WinAPI.

I spent more than 3 hours trying to do this, but totally failed.

Could anyone help me?

Here is my code:

FILETIME *KernelTime;
// Or  struct _FILETIME *KernelTime
HANDLE Process = OpenProcess ( PROCESS_ALL_ACCESS, FALSE, 0);
// 0 is the PID of System Idle Process
GetProcessTimes (Process, NULL, NULL, KernelTime, NULL);

/*
   How to write here?
*/

double ElapsedProcessTime // Target !!

I think these might be useful for solving this problem:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683223%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724280(v=vs.85).aspx

Compiler: GCC-mingw32

Thanks.


回答1:


From here:

This code sample will output the current CPU usage data collected from the performance counters in Windows. The results are shown in percent where 100% means max usage and 0% means minimum usage.

#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <stdio.h>

//------------------------------------------------------------------------------------------------------------------
// Prototype(s)...
//------------------------------------------------------------------------------------------------------------------
int cpuusage(void);

//------------------------------------------------------------------------------------------------------------------
// getcpuload()
//   directly prints the CPU usage on screen. This function need to be called twice with a minimum of 1 seconds
//   delay (msdn guideline) to display something usefull.
//   Also returns the usage in percent 0-100 where 100 means the system is working at maximum capacity.
//   Note for multiprocessor systems:
//   If one CPU is working at max capacity the result will (if using (_total) for PdhAddCounter() ) show a maximum
//   workload of 50% unless the other CPU(s) is also working at max load. 
//------------------------------------------------------------------------------------------------------------------
INT getcpuload()
{
  static PDH_STATUS            status;
  static PDH_FMT_COUNTERVALUE  value;
  static HQUERY                query;
  static HCOUNTER              counter;
  static DWORD                 ret;
  static char                  runonce=1;
  char                         cput=0;

  if(runonce)
  {
    status = PdhOpenQuery(NULL, 0, &query);
    if(status != ERROR_SUCCESS)
    {
      printf("PdhOpenQuery() ***Error: 0x%X\n",status);
      return 0;
    }

    PdhAddCounter(query, TEXT("\\Processor(_Total)\\% Processor Time"),0,&counter); // A total of ALL CPU's in the system
    //PdhAddCounter(query, TEXT("\\Processor(0)\\% Processor Time"),0,&counter);    // For systems with more than one CPU (Cpu0)
    //PdhAddCounter(query, TEXT("\\Processor(1)\\% Processor Time"),0,&counter);    // For systems with more than one CPU (Cpu1)
    runonce=0;
    PdhCollectQueryData(query); // No error checking here
    return 0;
  }

  status = PdhCollectQueryData(query);
  if(status != ERROR_SUCCESS)
  {
    printf("PhdCollectQueryData() ***Error: 0x%X\n",status);
    if(status==PDH_INVALID_HANDLE) 
      printf("PDH_INVALID_HANDLE\n");
    else if(status==PDH_NO_DATA)
      printf("PDH_NO_DATA\n");
    else
      printf("Unknown error\n");
    return 0;
  }

  status = PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100 ,&ret, &value);
  if(status != ERROR_SUCCESS)
  {
    printf("PdhGetFormattedCounterValue() ***Error: 0x%X\n",status);
    return 0;
  }
  cput = value.doubleValue;

  printf("\n\n"
         "CPU Total usage: %3d%%\n",cput);

  return cput;
}

//------------------------------------------------------------------------------------------------------------------
// Entry point
//------------------------------------------------------------------------------------------------------------------
int main(void)
{
  while(1)
  {
    getcpuload();
    sleep(1000);
  }
  return 0;
}


来源:https://stackoverflow.com/questions/20033457/problems-in-getting-process-time-in-c-please-help-me

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!