如何获取某个进程的cpu时间?

loki2k 2004-11-15 05:03:25
如何获取某个进程的cpu时间?即任务管理器中,进程页面有一栏叫“cpu时间”,请问该如何获得?
...全文
204 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
an_8 2004-11-16
  • 打赏
  • 举报
回复
看看<<windows 核心编程>>进程部分,能解决你的问题
loki2k 2004-11-16
  • 打赏
  • 举报
回复
你可以看看windows的任务管理器,进程一页有每一个进程用的cpu时间,一般最多是system idle process,也就是空闲状态,我现在需要的就是这一列的cpu时间,请各位大哥不吝赐教!
owsxo 2004-11-15
  • 打赏
  • 举报
回复
请教一下 在剥夺式多任务系统里 CPU不是按时间片分配使用的吗
那么取得使用的时间有什么意思呢
只有在巨型或大型机上,按照CPU的资源使用率来计算才是有意义的呀(用来计算费用)
不是吗?
zzxenjoy 2004-11-15
  • 打赏
  • 举报
回复
ding
loki2k 2004-11-15
  • 打赏
  • 举报
回复
liufeng24(流枫) 老兄,我没怎么看懂你的意思,NtQuerySystemInformation这个函数那里可以找到?
另:GetProcessTimes不能取到cpu时间,
BOOL GetProcessTimes(
HANDLE hProcess, // specifies the process of interest
LPFILETIME lpCreationTime, // when the process was created
LPFILETIME lpExitTime, // when the process exited
LPFILETIME lpKernelTime, // time the process has spent in kernel
// mode
LPFILETIME lpUserTime // time the process has spent in user mode
);

EnochShen 2004-11-15
  • 打赏
  • 举报
回复
来晚了,汗!~~这个函数必须要在200以后的版本
DentistryDoctor 2004-11-15
  • 打赏
  • 举报
回复
API:
GetProcessTimes
liufeng24 2004-11-15
  • 打赏
  • 举报
回复
// get system time
status = NtQuerySystemInformation(3,&SysTimeInfo,sizeof(SysTimeInfo),0);

if (status!=NO_ERROR)
{
MessageBox("failed to get system time!");
}

// get cpu idle time
status = NtQuerySystemInformation(2,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
if (status != NO_ERROR)
{
MessageBox("failed to get cpu time");
}

if (liOldIdleTime.QuadPart != 0)
{
// new cpu Time = NewTime - OldTime
dbCpuIdleTime =LI64ToDouble(SysPerfInfo.liIdleTime)-LI64ToDouble(liOldIdleTime);
dbSysTime =LI64ToDouble(SysTimeInfo.liKeSystemTime)-LI64ToDouble(liOldSysTime);
//get cpu usage
unCpuUsage=100 - 100*(dbCpuIdleTime/dbSysTime)/(double)SysBaseInfo.bKeNumberProcessors+0.5;
}

wsprintf(CpuUsage,"%d%%",(UINT)unCpuUsage);
m_sCpuUsage=(CString)CpuUsage;

//if cpu usage ==0% then shutdown os
if(bSetAuto&&(UINT)dbCpuIdleTime==0)
{
//MessageBox("shutdown os!");
ShutDown();
}
// store new cpu idle and system time
liOldIdleTime = SysPerfInfo.liIdleTime;
liOldSysTime = SysTimeInfo.liKeSystemTime;

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧