进程完成后的CPU时间
Linux 中是否有一个函数可以让我查看进程完成后使用了多少 CPU?我需要类似于 bash“time”命令的东西。我正在 fork() 进程,然后使用 wait() 等待子进程完成。准确测量“真实”时间(fork() 和 exit() 之间经过的实际时间)的方法,即使在子进程变成僵尸很久之后调用 wait() 也是受欢迎的,但我不确定它是否可能的。
Is there a function in Linux which allows me to see how much CPU did a process use after it finished? I need something similar to bash "time" command. I am fork()ing the process and then waiting using wait() for a child to finish. The way of accurately measuring "real" time (actual time elapsed between fork() and exit()), even when wait() was called a long after the child process became zombie is also welcome, but I'm not sure if its possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,
wait3
< /a> 和wait4
满足您的需求。或者(更便携)您可以使用getrusage(2)
。示例:
wait3
示例:
getrusage
当然,
wait3
和wait4
只是为了方便。所以你可以使用 getrusage :缺点是这会告诉你所有终止的子进程使用的资源。
那么,一旦你得到它,你该如何处理 rusage 呢?
struct rusage
具有以下形式:Sure,
wait3
andwait4
have you covered. Alternatively (and more portably) you could usegetrusage(2)
.Example:
wait3
Example:
getrusage
Of course,
wait3
andwait4
are just a convenience. So you could usegetrusage
:The disadvantage is that this tells you the resources used by ALL the terminated children.
So, once you get it, what do you do with
rusage
?struct rusage
has the following form:bash 功能“times”报告 shell 及其子级使用的总用户/系统时间。遗憾的是,此功能不报告总内存、I/O 等。IE:它不使用 getrusage(它应该使用)。
/usr/bin/time 程序将为您提供执行时间、内存占用量。因此,您可以执行 /usr/bin/time bash myprog.sh ... 并获取所有子进程的累积时间。
The bash feature "times" reports the total user/system times used by the shell and its children. This feature, unfortunately, doesn't report total memory, i/o etc. IE: it doesn't employ getrusage (which it should).
The /usr/bin/time program will give you executon time, memory footprint. So you can do /usr/bin/time bash myprog.sh ... and get the accumulated times for all children.