In the ps command, % CPU refers to the percentage of CPU usage time of a process. What does it mean? The man manual of PS is explained as follows:
CPU utilization of the Process in "##.. #" format. Currently, it is the CPU time used divided by the time the process has been running (cputime/realtime ratio ),Expressed as a percentage. It will not add up to 100% unless you are lucky. (alias pcpu ).
PS code is handled as follows:
Static int pr_pcpu (char * restrict const outbuf, const proc_t * restrict const pp) {unsigned long total_time;/* jiffies used by this process */unsigned pcpu = 0; /* scaled % CPU, 999 means 99.9% */unsigned long seconds;/* seconds of process life */total_time = PP-> utime + PP-> stime; If (include_dead_children) total_time + = (PP-> cutime + PP-> cstime); seconds = seconds_since_boot-PP-> start_time/Hertz; If (seconds) pcpu = (total_time * 1000ull/hertz) /seconds; If (pcpu> 999u) return snprintf (outbuf, colwid, "% u", pcpu/10u); Return snprintf (outbuf, colwid, "% u. % u ", pcpu/10u, pcpu % 10u );}
Where
Seconds_since_boot is obtained by subtracting the current time from the system start time. the start time is obtained by reading the btime in/proc/STAT. While
Start_time is set when a process is fork. In addition, the process time includes the user-mode running time and kernel-mode running time. In this way, the percentage indicates that the process is created to execute
The ratio of the running time of the process to that of the process within the period t during which the PS operation is performed.
If the include_dead_children option is specified in PS
Including the running time of the created but dead processes, cutime and cstime will call wait_task_zombie to accumulate when the parent process is collecting dead processes for the child process. Ratio
For example, if updatedb is executed in bash, run
PS-EO pcpu, comm, stat, PID | grep bash
And
PS-EO pcpu, comm, stat, PID | grep bash