【发布时间】:2020-11-20 23:50:11
【问题描述】:
对于一个学校项目,我需要在 C 中重新编写 ps aux 命令(如下图所示):
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 168028 12052 ? Ss 06:20 0:07 /sbin/init splash
root 2 0.0 0.0 0 0 ? S 06:20 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 06:20 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 06:20 0:00 [rcu_par_gp]
root 6 0.0 0.0 0 0 ? I< 06:20 0:00 [kworker/0:0H-kblockd]
root 9 0.0 0.0 0 0 ? I< 06:20 0:00 [mm_percpu_wq]
root 10 0.0 0.0 0 0 ? S 06:20 0:00 [ksoftirqd/0]
root 11 0.0 0.0 0 0 ? I 06:20 0:03 [rcu_sched]
root 12 0.0 0.0 0 0 ? S 06:20 0:00 [migration/0]
root 13 0.0 0.0 0 0 ? S 06:20 0:00 [idle_inject/0]
root 14 0.0 0.0 0 0 ? S 06:20 0:00 [cpuhp/0]
root 15 0.0 0.0 0 0 ? S 06:20 0:00 [cpuhp/1]
root 16 0.0 0.0 0 0 ? S 06:20 0:00 [idle_inject/1]
root 17 0.0 0.0 0 0 ? S 06:20 0:00 [migration/1]
root 18 0.0 0.0 0 0 ? S 06:20 0:00 [ksoftirqd/1]
root 20 0.0 0.0 0 0 ? I< 06:20 0:00 [kworker/1:0H-kblockd]
root 21 0.0 0.0 0 0 ? S 06:20 0:00 [cpuhp/2]
root 22 0.0 0.0 0 0 ? S 06:20 0:00 [idle_inject/2]
root 23 0.0 0.0 0 0 ? S 06:20 0:00 [migration/2]
root 24 0.0 0.0 0 0 ? S 06:20 0:00 [ksoftirqd/2]
我设法重新编程了除 %CPU 和 %MEM 之外的所有列。 和另一个一样,我知道我的答案在 /proc//[stat|status] 中,但我不知道我需要采用什么值以及计算百分比的公式是什么。
有什么想法吗?
【问题讨论】:
-
请不要发布文字图片 - reasoning。将其作为格式化文本复制到问题中。