linux 进程占用cpu查看工具,Linux下如何查看某一进程的CPU占用率

/proc下每个进程有一个目录,目录下的stat文件可以得到关于该进程的信息,比如:

# cat /proc/2122/stat

2122 (sshd) S 6599 2122 2122 0 -1 8448 1110 217 0 1 20 3 12 0 15 0 1 0 242306177 8482816 592 4294967295 134512640 134795532 3221224656 3221220240 4294960144 0 0 4096 73728 0 0 0 17 0 0 0

具体每一项的内容可以看man:

man 5 proc

cpu的占用率似乎并不能直接得到,你需要自己去计算,我猜想ps的做法是从/proc得到所有进程的信息,通过每个进程的运行时间,可以计算出某个进程的占用率

正好做过点这个,给你参考一下,恕未整理:

char buf[64];

sprintf(buf,"/proc/%d/stat",pid);

FILE* fid = fopen(buf,"r");

if(fid){

fscanf(fid,"%d %s %c %d %d %d %d %d %lu %lu "

"%lu %lu %lu %lu %lu %ld %ld %ld %ld %ld "

"%ld %ld %lu %lu %ld %lu %lu %lu %lu %lu",

&pstat[0],buf,&pstat[2],&pstat[3],&pstat[4],&pstat[5],&pstat[6],&pstat[7],

&pstat[8],&pstat[9],&pstat[10],&pstat[11],&pstat[12],&pstat[13],&pstat[14],&pstat[15],

&pstat[16],&pstat[17],&pstat[18],&pstat[19],&pstat[20],&pstat[21],&pstat[22],&pstat[23],

&pstat[24],&pstat[25],&pstat[26],&pstat[27],&pstat[28],&pstat[29],&pstat[30]);

fclose(fid);

。。。。。

ut = pstat[13];

st = pstat[14];

cut = pstat[15];

cst = pstat[16];

。。。。。

ts = time(NULL) - lasttime;

百分比=(ut+st-sut-sst)/ts

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值