&引用;ps aux";C语言中的命令

&引用;ps aux";C语言中的命令,c,pid,stat,ps,C,Pid,Stat,Ps,对于学校项目,我需要用C语言重新编程ps aux命令(如下图所示): USER PID%CPU%MEM VSZ RSS TTY STAT START TIME命令 根1 0.0 0.0 168028 12052?Ss 06:20 0:07/sbin/init飞溅 根2 0.0 0.0?S 06:20 0:00[kthread] 根3 0.0 0.0?Istart_time=field#22 of/proc/[pid]/stat 自启动后的秒数=字段#1/proc/uptime 如果传递了“S”选

对于学校项目,我需要用C语言重新编程
ps aux
命令(如下图所示):

USER PID%CPU%MEM VSZ RSS TTY STAT START TIME命令
根1 0.0 0.0 168028 12052?Ss 06:20 0:07/sbin/init飞溅
根2 0.0 0.0?S 06:20 0:00[kthread]
根3 0.0 0.0?I<06:20 0:00[区域协调单位gp]
根4 0.0 0.0?I<06:20 0:00[区域协调单位标准配置文件]
根60.0 0.0?I<06:20 0:00[kworker/0:0H-kblockd]
根9 0.0 0.0?I<06:20 0:00[mm_perpu_wq]
根10 0.0 0.0?S 06:20 0:00[ksoftirqd/0]
根11 0.0 0.0?I 06:20 0:03[rcu_sched]
根120.0 0.0?S 06:20 0:00[迁移/0]
根13 0.0 0.0?S 06:20 0:00[空转/0]
根14 0.0 0.0 0?S 06:20 0:00[cpuhp/0]
根15 0.0 0.0?S 06:20 0:00[cpuhp/1]
根16 0.0 0.0?S 06:20 0:00[空转/1]
根17 0.0 0.0?S 06:20 0:00[迁移/1]
根18 0.0 0.0?S 06:20 0:00[ksoftirqd/1]
根20 0.0 0.0?I<06:20 0:00[kworker/1:0H-kblockd]
根21 0.0 0.0?S 06:20 0:00[cpuhp/2]
根22 0.0 0.0?S 06:20 0:00[闲置/2]
根23 0.0 0.0?S 06:20 0:00[迁移/2]
根24 0.0 0.0?S 06:20 0:00[ksoftirqd/2]
除了%CPU%MEM之外,我设法重新编程了所有列。 和另一个一样,我知道我的答案在/proc/[stat | status]中,但我不知道需要取什么值,以及计算百分比的公式是什么


有什么想法吗?

您显示的内容取决于您模拟的
ps
命令

BSD
ps
手册页上有关于%CPU的说明:

进程的CPU利用率;这是前一(实时)时间长达一分钟的衰减平均值。由于计算该值的时基不同(某些进程可能非常年轻),因此所有%cpu字段的总和可能超过100%

%mem的描述如下:

此进程使用的实际内存百分比

注意它说的是“真实”内存,我认为这意味着虚拟内存被排除在计算之外。

看看of
ps
命令

%中央处理器 在以下方面:

  • pp->utime=field#14 of/proc/[pid]/stat
  • pp->stime=field#15 of/proc/[pid]/stat
  • pp->cutime=field#16 of/proc/[pid]/stat
  • pp->cstime=field#17 of/proc/[pid]/stat
  • pp->start_time=field#22 of/proc/[pid]/stat
  • 自启动后的秒数=字段#1/proc/uptime
  • 如果传递了“S”选项,则include_dead_children=1(汇总一些信息,例如从死子进程到其父进程的CPU使用情况)
[…]
#定义cook_etime(P)((无符号长时间)自启动以来的秒数>=(P->启动时间/赫兹))?((无符号长时间)自启动以来的秒数-(P->启动时间/赫兹)):0)
[...]
赫兹=sysconf(_SC_CLK_TCK);
[...]
/*此过程使用的jiffies*/
总时间=pp->utime+pp->time;
如果(包括死亡儿童)总时间+=(pp->cutime+pp->cstime);
/*工艺寿命的秒数*/
秒=烹饪时间(pp);
/*按比例%cpu,999表示99.9%*/
如果(秒)pcpu=(总时间*1000ULL/赫兹)/秒;
如果(pcpu>999U)
返回snprintf(EXBUF,COLWID,“%u”,pcpu/10U);
返回snprintf(EXBUF、COLWID、%u.%u、pcpu/10U、pcpu%10U);
[...]
%记忆 在以下方面:

  • pp->vm_rss=field#24 of/proc/[pid]/stat
  • kb\u main\u total=MemTotal字段/proc/meminfo
/*pp->vm_rss*1000会在具有64 GB内存的32位系统上溢出*/
pmem=pp->vm\u rss*1000ULL/kb\u总容量;
如果(pmem>999)pmem=999;
snprintf(EXBUF,COLWID,“%2u.%u”,(未签名)(pmem/10),(未签名)(pmem%10));

请不要发布文本的图像-。将它作为格式化文本复制到问题中。你想要我认为问题是如何获得这些信息
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]