如何检测进程cpu /内存使用情况,并在超过某个值时终止它? [英] How to detect a process cpu/memory usage and kill it when it exceeds a certain value?
问题描述
我在学习c / c ++,我想知道是否可以通过它的名称来检测进程,并且当它的cpu /内存使用超过一定值时,杀死它。
i'm learning c/c++ and i'm wondering if it is possible to detect a process by it's name and kill it when it's cpu/memory usage exceed a certain value. I would apreciate any help with the actual code or just pointing me in the right direction.
推荐答案
对于Windows和c ++:
For Windows and c++:
查看 GetProcessMemoryInfo 。 msdn 还有一个示例。
Check out GetProcessMemoryInfo. There's also an example at msdn.
GetProcessMemoryInfo()
函数将为您提供指向 PROCESS_MEMORY_COUNTERS struct包含所有内存使用信息。
The GetProcessMemoryInfo()
function will provide you a pointer to a PROCESS_MEMORY_COUNTERS struct which contains all of the memory usage information.
对于CPU使用情况来说有点困难,但是下面的stackoverflow问题与之相关:在c ++ / windows中获取特定进程的当前cpu使用情况
For CPU usage it's a little bit more difficult but the following stackoverflow question is related to that: Getting current cpu usage in c++/windows for particular process
这篇关于如何检测进程cpu /内存使用情况,并在超过某个值时终止它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!