gpt4 book ai didi

c - 如何计算C中的进程CPU使用率?

转载 作者:太空宇宙 更新时间:2023-11-04 04:54:30 25 4
gpt4 key购买 nike

我正在编写一个 Process Explorer 项目。
我可以使用 Win32 工具帮助快照获取有关进程的一些信息。
但是我无法计算每个进程的CPU使用量。
这是一个 C 窗口控制台应用程序。

最佳答案

也许 ISO C 标准化的 clock() 函数可以满足您的需求。

$ man clock

NAME clock — determine processor time used

LIBRARY Standard C Library (libc, -lc)

SYNOPSIS #include < time.h>

 clock_t
clock(void);

DESCRIPTION The clock() function determines the amount of processor time used since the invocation of the calling process, measured in CLOCKS_PER_SECs of a second.

RETURN VALUES The clock() function returns the amount of time used unless an error occurs, in which case the return value is -1.

您可以在 main() 开始时调用它一次,保存结果,然后在计算时调用(clock() - result)/CLOCKS_PER_SEC(注意整数除法的舍入!)。

关于c - 如何计算C中的进程CPU使用率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10617776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com