监控子进程/我可以获得进程组的 TotalProcessorTime 吗?
有没有办法获取反映进程加上它所生成的任何进程的Process.TotalProcessorTime?
或者,我如何验证该进程(或其后代)是否仍在“主动”运行?
我的应用程序正在生成一个外部进程并等待它退出。 对于示例数据,需要 5-20 分钟才能完成; 我无法猜测合理的最大超时时间。 因此,我的应用程序会定期检查其他进程的 Process.TotalProcessorTime 值,以确保它不断上升。
这工作正常,但我发现该进程只是一个生成子进程的“包装器”,而子进程又生成了一些消耗所有 CPU 时间的其他子进程。
我发现 TotalProcessorTime 在 CPU 利用率达到 100% 几分钟后仅返回一小部分秒。
Is there a way to get the Process.TotalProcessorTime that reflects a process PLUS any processes that it has spawned?
Alternatively, how can I verify that the process (or it's descendants) are still "actively" running?
My app is spawning an external process and waiting for it to exit. With sample data it takes 5-20 minutes to complete; I don't have a guess for a reasonable maximum timeout. So my app checks the Process.TotalProcessorTime value on the other process at intervals, to ensure that it keeps rising.
This works fine, but I've discovered that the process is simply a "wrapper" that spawns a sub-process, which in turn spawns a handful of other sub-process that consume all of the CPU time.
And I've found that TotalProcessorTime returns only a small fraction of a second after several minutes of 100% CPU utilization.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有实现这个,但已经弄清楚它是如何工作的。 这很乏味,但并不那么复杂。
生成一个后台线程,每隔几秒就会:
这应该允许调用代码轻松检查进程(树)是否
未挂起(I/O 绑定进程可能会出现问题,但这并不适用
对于我的情况)。
这一切似乎都不困难,除了获取不是由监视线程(直接)生成的进程的 CPU 使用情况之外。
如果/当我实现代码时,我会更新这个答案,但这可能需要很长时间。
I haven't implemented this, but have figured out how it might work. It's tedious but not all that complicated.
Spawn a background thread that every few seconds will:
This should allow the calling code to easily check that the process (tree) is
not hung (I/O bound processes might have problems here, but that doesn't apply
to my case).
None of this seems difficult, with the possible exception of getting CPU usage of a process not spawned (directly) by the monitoring thread.
I'll update this answer if/when I implement the code, but it may be a long time.
您可以使用性能计数器来检索父进程,如下所示:
有了该信息,您可以监视系统中的进程,维护整个进程树的结构并计算树的 TotalProcessorTime。 希望有帮助
You can use a performance counter to retrieve the parent process like this:
Having that information you can monitor processes in the system, maintain a structure of the whole process tree and calculate TotalProcessorTime for tree. Hope that helps