获取C中的当前进程CPU使用率

时间:2008-08-27 06:11:03

标签: c unix cross-platform process-management

在Windows上,我可以这样做:

HANDLE hProcess = GetCurrentProcess();

FILETIME ftCreation, ftExit, ftKernel, ftUser;

GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser);

SYSTEMTIME stKernel;
FileTimeToSystemTime(&ftKernel, &stKernel);

SYSTEMTIME stUser;
FileTimeToSystemTime(&ftUser, &stUser);

printf("Time in kernel mode = %uh %um %us %ums", stKernel.wHour,
           stKernel.wMinute, stKernel.wSecond, stKernel.wMilliseconds));
printf("Time in user mode = %uh %um %us %ums", stUser.wHour,
           stUser.wMinute, stUser.wSecond, stUser.wMilliseconds));

我怎样才能在* nix上做同样的事情?

1 个答案:

答案 0 :(得分:3)

检查getrusage,我认为这应该可以解决您的问题。