在得到处理时间用C的问题(请帮我) [英] Problems in getting Process Time in C ( Please help me )

查看:89
本文介绍了在得到处理时间用C的问题(请帮我)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C和WinAPI的一个新手。

I am a newbie in C and WinAPI.

我花了3个多小时试图做到这一点,但完全失败了。

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

谁能帮助我?

下面是我的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

编译器:GCC-的mingw32

Compiler: GCC-mingw32

感谢。

推荐答案

这里

这code样本将输出在Windows的性能计数器收集当前的CPU使用率数据。结果列于百分之示出,其中100%表示最大使用率和0%是指最低使用

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;
}

这篇关于在得到处理时间用C的问题(请帮我)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆