#include "CheckProcessCpu.h"
#include <string.h>
#include <stdlib.h>
#ifdef WIN32
#include <conio.h>
#include <TlHelp32.h>
#include <process.h>
#define MY_PROCESS_ERROR(Condition) do{ if (!(Condition)) goto Exit0; } while (false)
static DWORD g_sdwTickCountOld = 0; // ??һ?ε?tick????
static LARGE_INTEGER g_slgProcessTimeOld; // ??????????һ?ε?ʱ??ռ??
static DWORD g_sdwProcessorCoreNum = 0; // ????????????
static HANDLE g_shExitEvent = NULL; // ?߳??˳?????
int GetProcessCpuPercent(const HANDLE hProcess, const DWORD dwElepsedTime)
{
int nProcCpuPercent = 0;
BOOL bRetCode = FALSE;
FILETIME CreateTime, ExitTime, KernelTime,UserTime;
LARGE_INTEGER lgKernelTime;
LARGE_INTEGER lgUserTime;
LARGE_INTEGER lgCurTime;
bRetCode = GetProcessTimes(hProcess, &CreateTime, &ExitTime, &KernelTime, &UserTime);
if (bRetCode)
{
lgKernelTime.HighPart = KernelTime.dwHighDateTime;
lgKernelTime.LowPart = KernelTime.dwLowDateTime;
lgUserTime.HighPart = UserTime.dwHighDateTime;
lgUserTime.LowPart = UserTime.dwLowDateTime;
lgCurTime.QuadPart = (lgKernelTime.QuadPart + lgUserTime.QuadPart) / 10000;
nProcCpuPercent = (int)((lgCurTime.QuadPart - g_slgProcessTimeOld.QuadPart) * 100 / dwElepsedTime);
g_slgProcessTimeOld = lgCurTime;
nProcCpuPercent = nProcCpuPercent / g_sdwProcessorCoreNum;
}
else
{
nProcCpuPercent = -1;
}
return nProcCpuPercent;
}
unsigned int BeforeProcIDCpuPercent(DWORD ProcessID)
{
HRESULT hr = E_FAIL;
HANDLE hProcess = NULL;
DWORD dwProcessId = ProcessID;
DWORD dwRetVal = 0;
DWORD dwCurrentTickCount = 0;
DWORD dwElapsedTime = 0;
int nProcessCpuPercent = 0;
//TARGET_PROCESS *pTargetProcess = (TARGET_PROCESS *)pArg;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION , FALSE, dwProcessId);
MY_PROCESS_ERROR(hProcess);
dwRetVal = WaitForSingleObject(g_shExitEvent, 200);
dwCurrentTickCount = GetTickCount();
dwElapsedTime = dwCurrentTickCount - g_sdwTickCountOld;
g_sdwTickCountOld = dwCurrentTickCount;
nProcessCpuPercent = GetProcessCpuPercent(hProcess, dwElapsedTime);
dwRetVal = WaitForSingleObject(g_shExitEvent, 200);
if (WAIT_OBJECT_0 == dwRetVal ||
WAIT_FAILED == dwRetVal
)
{
goto Exit0;
}
dwCurrentTickCount = GetTickCount();
dwElapsedTime = dwCurrentTickCount - g_sdwTickCountOld;
g_sdwTickCountOld = dwCurrentTickCount;
nProcessCpuPercent = GetProcessCpuPercent(hProcess, dwElapsedTime);
return nProcessCpuPercent;
Exit0:
if (hProcess)
{
CloseHandle(hProcess);
hProcess = NULL;
}
return -1;
}
unsigned int ProcIDCpuPercent(DWORD ProcessID)
{
SYSTEM_INFO sysInfo = { 0 };
GetSystemInfo(&sysInfo);
g_sdwProcessorCoreNum = sysInfo.dwNumberOfProcessors;
g_shExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!g_shExitEvent)
{
CloseHandle(g_shExitEvent);
g_shExitEvent = NULL;
return -1;
}
return BeforeProcIDCpuPercent(ProcessID);
}
HRESULT GetAllProcCpuPercent(const TCHAR* cpszExeFileName, unsigned int *proc_ret,unsigned int *proc_nums)
{
HRESULT hr = E_FAIL;
DWORD dwPID;
PROCESSENTRY32 pe = { 0 };
HANDLE hSnapshot = NULL;
if (NULL == cpszExeFileName)
{
hr = HRESULT_FROM_WIN32(ERROR_BAD_ARGUMENTS);
goto Exit0;
}
pe.dwSize = sizeof(PROCESSENTRY32);
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
hr = HRESULT_FROM_WIN32(GetLastError());
goto Exit0;
}
if (FALSE == Process32First(hSnapshot, &pe))
{
hr = HRESULT_FROM_WIN32(ERROR_NO_MORE_FILES);
goto Exit0;
}
hr = S_FALSE;
do
{
if (0 == strcmp(cpszExeFileName, pe.szExeFile))
{
dwPID = pe.th32ProcessID;
(*proc_nums) ++;
(*proc_ret) +=ProcIDCpuPercent(dwPID);
}
}while(Process32Next(hSnapshot, &pe));
hr = S_OK;
Exit0:
if(hSnapshot)
{
CloseHandle(hSnapshot);
hSnapshot = NULL;
}
return hr;
}
void CheckProcessCpu(const char *process_name,unsigned int *res,unsigned int *proc_nums)
{
*res =0;
*proc_nums =0;
GetAllProcCpuPercent(process_name,res,proc_nums);
}
#else
#ifndef PROC_DIRECTORY
#define PROC_DIRECTORY "/proc/"
#endif
static int IsNumeric(const char* ccharptr_CharacterList)
{
for ( ; *ccharptr_CharacterList; ccharptr_CharacterList++)
if (*ccharptr_CharacterList < '0' || *ccharptr_CharacterList > '9')
return 0; // false
return 1; // true
}
void CheckProcessCpu(const char *process_name,unsigned int *res,unsigned int *proc_nums)
{
*res = 0;
*proc_nums = 0;
DIR *dir_proc = NULL;
char status_path[100] ="\0";
char proc_name_flag[100] = "\0";
char proc_name[64]= "\0";
char proc_pid_flag[100] = "\0";
unsigned int proc_pid =0;
struct dirent* de_DirEntity = NULL ;
dir_proc = opendir(PROC_DIRECTORY) ;
if (dir_proc == NULL)
{
perror("Couldn't open the " PROC_DIRECTORY " directory") ;
return ;
}
// ???ݽ??????ƣ????????н???ID????????ռ??CPU????
while ( (de_DirEntity = readdir(dir_proc)) )
{
if (de_DirEntity->d_type == DT_DIR)
{
if (IsNumeric(de_DirEntity->d_name)){
strcpy(status_path, PROC_DIRECTORY) ;
strcat(status_path, de_DirEntity->d_name) ;
strcat(status_path, "/status") ;
FILE * fp = fopen(status_path,"rt");
if (fp)
{
fgets(proc_name_flag,sizeof(proc_name),fp);
sscanf(proc_name_flag,"%*s%s",proc_name);
if (!strcmp(proc_name,process_name))
{
fgets(proc_pid_flag,sizeof(proc_pid_flag),fp);
fgets(proc_pid_flag,sizeof(proc_pid_flag),fp);
fclose(fp);
sscanf(proc_pid_flag,"%*s%d",&proc_pid);
(*proc_nums)++;
(*res) += get_process_cpu_useage(proc_pid);
}
}
}
}
}
closedir(dir_proc);
}
#endif
C/C++实现linux和Windows查看系统、进程、服务的内存和CPU使用
5星 · 超过95%的资源 需积分: 1 86 浏览量
2014-08-21
15:33:01
上传
评论 10
收藏 16KB ZIP 举报
我还在迷路
- 粉丝: 85
- 资源: 10
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页