Bilal Syed Hussain
Bilal Syed Hussain

Reputation: 9204

Find cpu time of a process and it children while the process is still running

I have a bash script which runs a series of programs which run other programs . How would I get the cpu time of the bash script and all of it descendants processes.

I looked in /proc/{process-id}/stat and the question How to calculate the CPU usage of a process by PID in Linux from C?

but I am not sure how I would calculate the total cpu time for all the child processes of the bash script.

Edit If I calculate difference in utime and difference in stime for some time interval, I know for a single process the cpu time (utime + stime) it took in that interval.

How would I add up the cpu time for all the child processes (which also create their own processes) while the process is still running?

Upvotes: 2

Views: 1691

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

You can calculate it using the:

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

Upvotes: 2

Related Questions