Android命令之ps

来源:互联网 发布:淘宝皇冠要多久 编辑:程序博客网 时间:2024/06/05 07:01

Android系统基本都会内置ps命令,通过ps命令我们能简单的了解相应进程的stat。
源码在system/core/toolbox/ps.c

选项 解释 -t 显示所有线程的信息 -n 显示数字UID -Z 显示mac label,此选择是和其他选项对立的 -P 显示进程策略,存在三种策略fg、bg、空白 -p 对应的是prio, nice, rtprio, sched四种信息 -c 显示CPU占用率 –abi 显示进程位宽 数字 筛选指定数字pid 字符 筛选指定字符pid

ps通过解析以下这些文件获取相应的信息

"/proc/%d", pid

通过此获得uid,默认显示字符形式的UID、可以通过-n显示数字形式的UID

"/proc/%d/stat", pid

通过此读取pid的stat,最终筛选出PPID、VSIZE、RSS、WCHAN、PC、NAME。

读取出来的信息类似这样的:

18017 (du.map.location) S 17394 17394 0 0 -1 4194624 2970 0 0 0 6 12 0 0 20 0 10 0 42960889 438394880 9400 4294967295 1073958912 1073963167 3204012848 3204011168 1074637424 0 4612 0 38120 4294967295 0 0 17 0 0 0 0 0 0

以下是ps对pid的stat的解析过程,statline是读取出的全部信息,nexttok是读取下一个值。

ptr = statline;nexttok(&ptr); // skip pidptr++;         // skip "("name = ptr;ptr = strrchr(ptr, ')'); // Skip to *last* occurence of ')',*ptr++ = '\0';           // and null-terminate name.ptr++;          // skip " "state = nexttok(&ptr);ppid = atoi(nexttok(&ptr));nexttok(&ptr); // pgrpnexttok(&ptr); // sidnexttok(&ptr); // ttynexttok(&ptr); // tpgidnexttok(&ptr); // flagsnexttok(&ptr); // minfltnexttok(&ptr); // cminfltnexttok(&ptr); // majfltnexttok(&ptr); // cmajfltutime = atoi(nexttok(&ptr));stime = atoi(nexttok(&ptr));nexttok(&ptr); // cutimenexttok(&ptr); // cstimeprio = atoi(nexttok(&ptr));nice = atoi(nexttok(&ptr));nexttok(&ptr); // threadsnexttok(&ptr); // itrealvaluenexttok(&ptr); // starttimevss = strtoul(nexttok(&ptr), 0, 10); // vsizerss = strtoul(nexttok(&ptr), 0, 10); // rssnexttok(&ptr); // rlimnexttok(&ptr); // startcodenexttok(&ptr); // endcodenexttok(&ptr); // startstacknexttok(&ptr); // kstkespeip = strtoul(nexttok(&ptr), 0, 10); // kstkeipnexttok(&ptr); // signalnexttok(&ptr); // blockednexttok(&ptr); // sigignorenexttok(&ptr); // sigcatchwchan = strtoul(nexttok(&ptr), 0, 10); // wchannexttok(&ptr); // nswapnexttok(&ptr); // cnswapnexttok(&ptr); // exit signalpsr = atoi(nexttok(&ptr)); // processorrtprio = atoi(nexttok(&ptr)); // rt_prioritysched = atoi(nexttok(&ptr)); // scheduling policynexttok(&ptr); // tty
"/proc/%d/attr/current", pid

MACLABEL的信息就是通过读取此pid下的这个文件获取到的,如果没有内容默认赋值为-,此内容可以通过选项-Z来显示,此选项跟其他选项是对立存在的。

"/proc/%d/cmdline", pid

在显示MACLABEL的时候,如果对应的pid下cmdline下有内容,那么就用于替换NAME显示输出。

"/proc/%d/task/%d/stat", pid, tid

通过读取pid下的task来显示tid的信息,每个tid下的内容和pid的内容都是相似的。

0 0
原创粉丝点击