windows下如何知道子进程状态和资源使用情况?
我想编写一个程序,它将启动一个子进程。 子进程可以是windows模式或控制台模式程序。
我想监控子进程状态和资源使用情况。 例如我想知道子进程仍在运行或终止。 如果它终止,我想知道原因(正常终止还是因为崩溃?)。
在子进程运行和/或终止期间,我想知道其资源使用情况,特别是 CPU 时间(用户时间、系统)和内存使用情况(虚拟大小和/或 rss)。 如果数字不是很准确也没关系。
在 Unix 术语中,我想要 fork、exec、waitpid 和 getrusage 。 而fork+setrusage+exec可以限制child的资源使用。 但我不知道如何在Windows平台上执行这些操作。
请指出 Windows API 名称。 剩下的我可以自己研究。
最好不要使用 Windows API 以外的库。 希望它不是父进程作为调试器并附加到子进程。 只是不喜欢,但还是可以接受的。
I want to write a program, which will launch a child process. The child process may be windows mode or console mode program.
I want to monitor the child process status and resource usage. e.g. I want to know the child process is still running or terminated. If it terminated, I want to know the reason (is terminated normally or because of crash?).
And during the child process running and/or it terminated, I want to know its resource usage, especially CPU time (user time, system) and memory usage (virtual size and/or rss). It is OK if the numbers are not very accurate.
In Unix terminology, I want to fork, exec, waitpid and getrusage . And fork+setrusage+exec can limit child's resource usage. But I don't know how to do these on the Windows platform.
Please point me the Windows API name. I could study the rest myself.
Prefer not using library other than the Windows API. Prefer it is not parent working as debugger and attaching to child process. Just not prefer, but still acceptable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用 CreateProcess 时,它会返回该进程的句柄。
进程句柄上的 WaitForSingleObject 将阻塞,直到进程退出或超时已到。 超时为零将立即返回并指示进程是否仍在运行。
要获取正在运行的进程的退出代码,可以使用 GetExitCodeProcess。 但是,您需要解释错误代码的含义。 0xC0000005 是典型的访问冲突,但并非所有崩溃都会导致此错误代码。
对于资源使用情况,您可以调用 GetProcessTimes 获取总 CPU 时间,调用 GetGuiResources 获取 GDI 句柄信息,调用 GetProcessMemoryInfo 获取内存统计信息,调用 GetProcessIoCounters 获取 IO 信息。
When you call CreateProcess, it returns a handle to the process.
WaitForSingleObject on a process handle will block until the process has exited or time-out has expired. A timeout of zero will return immediately and indicate if the process is still running.
To get the exit code of a running process, you can use GetExitCodeProcess. You'll need to interpret what the error code means, however. 0xC0000005 is typical for an access violation, but not all crashes result in this error code.
For resource usage, you can call GetProcessTimes to get total CPU time, GetGuiResources to get GDI handle info, GetProcessMemoryInfo to get memory stats, and GetProcessIoCounters to get IO info.