linux下用c语言做成绩统计,如何用C语言在Linux中用PID计算进程的CPU使用量?

729c8baed008382a7d70f3c6a357b66e.png

精慕HU

您需要从/proc//stat..这些是最初的几个字段Documentation/filesystems/proc.txt在您的内核源代码中):Table 1-3: Contents of the stat files (as of 2.6.22-rc3).............................................................................. Field          Content  pid           process id  tcomm         filename of the executable  state         state (R is running, S is sleeping, D is sleeping in an                uninterruptible wait, Z is zombie, T is traced or stopped)  ppid          process id of the parent process  pgrp          pgrp of the process  sid           session id  tty_nr        tty the process uses  tty_pgrp      pgrp of the tty  flags         task flags  min_flt       number of minor faults  cmin_flt      number of minor faults with child's  maj_flt       number of major faults  cmaj_flt      number of major faults with child's  utime         user mode jiffies  stime         kernel mode jiffies  cutime        user mode jiffies with child's  cstime        kernel mode jiffies with child's你可能是在找utime和/或stime..您还需要阅读cpu从/proc/stat,看起来是:cpu  192369 7119 480152 122044337 14142 9937 26747 0 0这将告诉您在各种类别中使用的累计CPU时间(单位为Jiffies)。您需要取这一行上的值之和,才能获得time_total测量。两者都读utime和stime对于您感兴趣的过程,请阅读time_total从…/proc/stat..然后睡一会儿左右,然后再读一遍。现在,您可以通过以下方法计算采样时间内进程的CPU使用情况:user_util = 100 * (utime_after - utime_before) / (time_total_after - time_total_before);

sys_util = 100 * (stime_after - stime_before) / (time_total_after - time_total_before);讲得通?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值