C语言中获取当前进程CPU使用率
在 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 上做同样的事情?
On Windows I can do:
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));
How can I do the same thing on *nix?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查 getrusage,我认为这应该可以解决您的问题。
Check getrusage, I think that should solve your problem.