gpt4 book ai didi

c - 查找进程的 cpu 时间,并在进程仍在运行时查找它的子进程

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:21:44 25 4
gpt4 key购买 nike

我有一个 bash 脚本,它运行一系列运行其他程序的程序。我如何获得 bash 脚本的 cpu 时间和它的所有后代进程。

我查看了/proc/{process-id}/stat 和问题 How to calculate the CPU usage of a process by PID in Linux from C?

但我不确定如何计算 bash 脚本的所有子进程的总 CPU 时间。

编辑如果我计算某个时间间隔内 utime 的差异和 stime 的差异,我知道单个进程在该时间间隔内花费的 cpu 时间(utime + stime)。

当进程仍在运行时,我如何将所有子进程(它们也创建自己的进程)的 cpu 时间相加?

最佳答案

您可以使用以下方法计算它:

utime %lu

Amount of time that this process has been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK). This includes guest time, guest_time (time spent running a virtual CPU, see below), so that applications that are not aware of the guest time field do not lose that time from their calculations.

stime %lu

Amount of time that this process has been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).

cutime %ld

Amount of time that this process's waited-for children have been scheduled in user mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK). (See also times(2).) This includes guest time, cguest_time (time spent running a virtual CPU, see below).

cstime %ld

Amount of time that this process's waited-for children have been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK).

关于c - 查找进程的 cpu 时间,并在进程仍在运行时查找它的子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20498654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com