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

发布于 2024-07-04 14:29:08 字数 662 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2024-07-11 14:29:08

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

Check getrusage, I think that should solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文