linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > article > Linux开发区 > SHELL >
栏目导栏
资料搜索
热门文章
·csh shell编程入门
·玩转Linux shell命令提示符
·Bourne Shell及shell编程
·Shell 编程实例集锦
·Linux下的shell编程入门
·linux shell 编程基础
·Shell编程基础
·Linux的Shell编程
·shell基础十二篇
·linux Shell编程入门 实例讲解
·Linux shell 脚本实例一
·深入浅出Shell编程: Shell 变量
·Linux主要shell命令详解
·UNIX/LINUX SHELL 正则表达式语
·shell命令(一)
最新文章
·Korn Shell 脚本入门
·linux下shell的工作原理
·Linux系统中加入自定义Shell为
·Shell学习:关于替换命令-tr-R
·Linux Shell学习:uniq命令使用
·uClinux操作系统下的shell功能
·Shell编程基础:单引号和双引号
·Linux操作系统下Shell语句元字
·Linux系统环境程序设计之路
·Linux Shell中PS命令中的%CPU的
·Linux Shell元字符知识笔记
·压缩命令-vi-认识SHELL-正规表
·Linux系统下Shell命令行快捷键
·谈Linux Shell下的输出重定向
·在Shell中执行vi/cp/mv时自动备
Google
 
Linux Shell中PS命令中的%CPU的含义介绍
[ 作者:  加入时间:2008-02-27 10:06:58  来自:Linux联盟收集整理 ]
 

PS命令中的%CPU是指一个进程占用CPU的时间百分比,那么具体的含义是什么呢?

PS的man手册的解释是这样的:

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的代码中是这样处理的

static int pr_pcpu(char *restrict const outbuf, const proc_t *restrict const pp){
unsigned long long total_time; /* jiffies used by this process */
unsigned pcpu = 0; /* scaled %cpu, 999 means 99.9% */
unsigned long 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);
}

其中seconds_since_boot是用当前时间减去系统启动时的时间得到的,启动的时间通过读/proc/stat中的btime获得。而start_time是进程被fork时设置的。另外进程的时间包括在用户态运行的时间和内核态运行的时间。这样,这个百分比的含义就是从进程被创建到执行ps操作这段时间T内,这个进程运行的时间和T的比值。

如果在ps中指定了include_dead_children选项,那么这个进程的时间还包括它的它创建的但已经死去的进程的运行时间,cutime和cstime会在父进程为子进程收尸的时候调用wait_task_zombie来累加。比如在bash中执行updatedb,在执行完成后,运行

ps -eo pcpu,comm,stat,pid|grep bash

ps S -eo pcpu,comm,stat,pid|grep bash

后者的百分比更在。

Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·Linux系统中Fstab文件的含义  (2008-02-26 09:53:54)
 ·Linux Shell元字符知识笔记  (2008-02-25 13:27:00)
 ·关于Linux操作系统文件扩展名含义的介绍  (2008-02-21 09:41:59)
 ·Linux服务器常出现的错误及其代表的含义  (2008-02-19 10:50:09)
 ·谈Linux Shell下的输出重定向  (2008-01-25 11:07:21)
 ·如何改变Linux Shell中的字体和背景颜色  (2008-01-21 11:43:22)
 ·Linux Shell中强大的正则grep  (2007-12-28 10:35:26)
 ·Linux Shell下的后台运行及其前台的转换  (2007-12-21 12:51:11)
 ·Linux Shell命令行中一些鲜为人知的秘密  (2007-12-14 11:17:14)
 ·取得一台机器的CPU占用率  (2007-12-03 14:36:39)