进程完成后的CPU时间

发布于 2024-11-29 07:21:51 字数 183 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溇涏 2024-12-06 07:21:51

当然,wait3< /a> 和 wait4 满足您的需求。或者(更便携)您可以使用 getrusage(2)

wait3()和wait4()系统调用与waitpid(2)类似,但是
另外返回有关子级的资源使用信息
rusage 指向的结构。

示例:wait3

struct rusage usage;
wait3(&status, 0, &usage);

示例:getrusage

当然,wait3wait4 只是为了方便。所以你可以使用 getrusage :

getrusage(RUSAGE_CHILDREN, &usage);

缺点是这会告诉你所有终止的子进程使用的资源。

那么,一旦你得到它,你该如何处理 rusage 呢? struct rusage 具有以下形式:

struct rusage {
    struct timeval ru_utime; /* user CPU time used */
    struct timeval ru_stime; /* system CPU time used */
    /* More. */
};

Sure, wait3 and wait4 have you covered. Alternatively (and more portably) you could use getrusage(2).

The wait3() and wait4() system calls are similar to waitpid(2), but
additionally return resource usage information about the child in the
structure pointed to by rusage.

Example: wait3

struct rusage usage;
wait3(&status, 0, &usage);

Example: getrusage

Of course, wait3 and wait4 are just a convenience. So you could use getrusage:

getrusage(RUSAGE_CHILDREN, &usage);

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:

struct rusage {
    struct timeval ru_utime; /* user CPU time used */
    struct timeval ru_stime; /* system CPU time used */
    /* More. */
};
小…楫夜泊 2024-12-06 07:21:51
  1. bash 功能“times”报告 shell 及其子级使用的总用户/系统时间。遗憾的是,此功能不报告总内存、I/O 等。IE:它不使用 getrusage(它应该使用)。

  2. /usr/bin/time 程序将为您提供执行时间、内存占用量。因此,您可以执行 /usr/bin/time bash myprog.sh ... 并获取所有子进程的累积时间。

  1. 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).

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文