解析ANDROID ps命令执行后各项参数的含义 .

如何查看ANDROID进程信息呢?

可以打开Adb shell,然后我们就有2种方法:


方法1:直接输入ps命令

输入之后,我们就可以看到如下的信息:

  1. # ps  
  2. ps  
  3. USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME  
  4. root      1     0     276    188   c0099f1c 000086e8 S /init  
  5. root      2     0     0      0     c004df64 00000000 S kthreadd  
  6. root      3     2     0      0     c003fa28 00000000 S ksoftirqd/0  
  7. root      4     2     0      0     c004abc0 00000000 S events/0  
  8. root      5     2     0      0     c004abc0 00000000 S khelper  
  9. root      6     2     0      0     c004abc0 00000000 S suspend  
  10. root      7     2     0      0     c004abc0 00000000 S kblockd/0  
  11. root      8     2     0      0     c004abc0 00000000 S cqueue  
  12. root      9     2     0      0     c01780d0 00000000 S kseriod  
  13. root      10    2     0      0     c004abc0 00000000 S kmmcd  
  14. root      11    2     0      0     c006efa8 00000000 S pdflush  
  15. root      12    2     0      0     c006efa8 00000000 S pdflush  
  16. root      13    2     0      0     c0073480 00000000 S kswapd0  
  17. root      14    2     0      0     c004abc0 00000000 S aio/0  
  18. root      22    2     0      0     c0175900 00000000 S mtdblockdc  
# ps
ps
USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
root      1     0     276    188   c0099f1c 000086e8 S /init
root      2     0     0      0     c004df64 00000000 S kthreadd
root      3     2     0      0     c003fa28 00000000 S ksoftirqd/0
root      4     2     0      0     c004abc0 00000000 S events/0
root      5     2     0      0     c004abc0 00000000 S khelper
root      6     2     0      0     c004abc0 00000000 S suspend
root      7     2     0      0     c004abc0 00000000 S kblockd/0
root      8     2     0      0     c004abc0 00000000 S cqueue
root      9     2     0      0     c01780d0 00000000 S kseriod
root      10    2     0      0     c004abc0 00000000 S kmmcd
root      11    2     0      0     c006efa8 00000000 S pdflush
root      12    2     0      0     c006efa8 00000000 S pdflush
root      13    2     0      0     c0073480 00000000 S kswapd0
root      14    2     0      0     c004abc0 00000000 S aio/0
root      22    2     0      0     c0175900 00000000 S mtdblockdc

那么我们禁不住要问:

USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME


那么这些项各代表着什么意思呢?

  1. USER:  进程的当前用户;
  2. PID   : 毫无疑问, process ID的缩写,也就进程号;
  3. PPID  :process parent ID,父进程ID
  4. VSIZE  : virtual size,进程虚拟地址空间大小;
  5. RSS    : 进程正在使用的物理内存的大小;
  6. WCHAN  :进程如果处于休眠状态的话,在内核中的地址;
  7. PC  : program counter,
  8. NAME: process name,进程的名称

 

android ps命令实现的源码

android下ps命令的源码的位置:android/system/core/toolbox/ps.c,其实现如下:

  1. int ps_main(int argc, char **argv)  
  2. {  
  3.     DIR *d;  
  4.     struct dirent *de;  
  5.     char *namefilter = 0;  
  6.     int pidfilter = 0;  
  7.     int threads = 0;  
  8.       
  9.     d = opendir("/proc");  
  10.     if(d == 0) return -1;  
  11.   
  12.     while(argc > 1){  
  13.         if(!strcmp(argv[1],"-t")) {  
  14.             threads = 1;  
  15.         } else if(!strcmp(argv[1],"-x")) {  
  16.             display_flags |= SHOW_TIME;  
  17.         } else if(!strcmp(argv[1],"-p")) {  
  18.             display_flags |= SHOW_PRIO;  
  19.         }  else if(isdigit(argv[1][0])){  
  20.             pidfilter = atoi(argv[1]);  
  21.         } else {  
  22.             namefilter = argv[1];  
  23.         }  
  24.         argc--;  
  25.         argv++;  
  26.     }  
  27.   
  28.     printf("USER     PID   PPID  VSIZE RSS   %sWCHAN    PC         NAME\n",   
  29.            (display_flags&SHOW_PRIO)?"PRIO  NICE  RTPRI SCHED ":"");  
  30.     while((de = readdir(d)) != 0){  
  31.         if(isdigit(de->d_name[0])){  
  32.             int pid = atoi(de->d_name);  
  33.             if(!pidfilter || (pidfilter == pid)) {  
  34.                 ps_line(pid, 0, namefilter);  
  35.                 if(threads) ps_threads(pid, namefilter);  
  36.             }  
  37.         }  
  38.     }  
  39.     closedir(d);  
  40.     return 0;  
  41. }  
int ps_main(int argc, char **argv)
{
    DIR *d;
    struct dirent *de;
    char *namefilter = 0;
    int pidfilter = 0;
    int threads = 0;
    
    d = opendir("/proc");
    if(d == 0) return -1;

    while(argc > 1){
        if(!strcmp(argv[1],"-t")) {
            threads = 1;
        } else if(!strcmp(argv[1],"-x")) {
            display_flags |= SHOW_TIME;
        } else if(!strcmp(argv[1],"-p")) {
            display_flags |= SHOW_PRIO;
        }  else if(isdigit(argv[1][0])){
            pidfilter = atoi(argv[1]);
        } else {
            namefilter = argv[1];
        }
        argc--;
        argv++;
    }

    printf("USER     PID   PPID  VSIZE RSS   %sWCHAN    PC         NAME\n", 
           (display_flags&SHOW_PRIO)?"PRIO  NICE  RTPRI SCHED ":"");
    while((de = readdir(d)) != 0){
        if(isdigit(de->d_name[0])){
            int pid = atoi(de->d_name);
            if(!pidfilter || (pidfilter == pid)) {
                ps_line(pid, 0, namefilter);
                if(threads) ps_threads(pid, namefilter);
            }
        }
    }
    closedir(d);
    return 0;
}


我们可以得到每一行数据是如何获得的:

  1. static int ps_line(int pid, int tid, char *namefilter)  
  2. {  
  3.     char statline[1024];  
  4.     char cmdline[1024];  
  5.     char user[32];  
  6.     struct stat stats;  
  7.     int fd, r;  
  8.     char *ptr, *name, *state;  
  9.     int ppid, tty;  
  10.     unsigned wchan, rss, vss, eip;  
  11.     unsigned utime, stime;  
  12.     int prio, nice, rtprio, sched;  
  13.     struct passwd *pw;  
  14.       
  15.     sprintf(statline, "/proc/%d", pid);  
  16.     stat(statline, &stats);  
  17.   
  18.     if(tid) {  
  19.         sprintf(statline, "/proc/%d/task/%d/stat", pid, tid);  
  20.         cmdline[0] = 0;  
  21.     } else {  
  22.         sprintf(statline, "/proc/%d/stat", pid);  
  23.         sprintf(cmdline, "/proc/%d/cmdline", pid);      
  24.         fd = open(cmdline, O_RDONLY);  
  25.         if(fd == 0) {  
  26.             r = 0;  
  27.         } else {  
  28.             r = read(fd, cmdline, 1023);  
  29.             close(fd);  
  30.             if(r < 0) r = 0;  
  31.         }  
  32.         cmdline[r] = 0;  
  33.     }  
  34.       
  35.     fd = open(statline, O_RDONLY);  
  36.     if(fd == 0) return -1;  
  37.     r = read(fd, statline, 1023);  
  38.     close(fd);  
  39.     if(r < 0) return -1;  
  40.     statline[r] = 0;  
  41.   
  42.     ptr = statline;  
  43.     nexttok(&ptr); // skip pid   
  44.     ptr++;          // skip "("   
  45.   
  46.     name = ptr;  
  47.     ptr = strrchr(ptr, ')'); // Skip to *last* occurence of ')',   
  48.     *ptr++ = '\0';           // and null-terminate name.   
  49.   
  50.     ptr++;          // skip " "   
  51.     state = nexttok(&ptr);  
  52.     ppid = atoi(nexttok(&ptr));  
  53.     nexttok(&ptr); // pgrp   
  54.     nexttok(&ptr); // sid   
  55.     tty = atoi(nexttok(&ptr));  
  56.       
  57.     nexttok(&ptr); // tpgid   
  58.     nexttok(&ptr); // flags   
  59.     nexttok(&ptr); // minflt   
  60.     nexttok(&ptr); // cminflt   
  61.     nexttok(&ptr); // majflt   
  62.     nexttok(&ptr); // cmajflt   
  63. #if 1   
  64.     utime = atoi(nexttok(&ptr));  
  65.     stime = atoi(nexttok(&ptr));  
  66. #else   
  67.     nexttok(&ptr); // utime   
  68.     nexttok(&ptr); // stime   
  69. #endif   
  70.     nexttok(&ptr); // cutime   
  71.     nexttok(&ptr); // cstime   
  72.     prio = atoi(nexttok(&ptr));  
  73.     nice = atoi(nexttok(&ptr));  
  74.     nexttok(&ptr); // threads   
  75.     nexttok(&ptr); // itrealvalue   
  76.     nexttok(&ptr); // starttime   
  77.     vss = strtoul(nexttok(&ptr), 0, 10); // vsize   
  78.     rss = strtoul(nexttok(&ptr), 0, 10); // rss   
  79.     nexttok(&ptr); // rlim   
  80.     nexttok(&ptr); // startcode   
  81.     nexttok(&ptr); // endcode   
  82.     nexttok(&ptr); // startstack   
  83.     nexttok(&ptr); // kstkesp   
  84.     eip = strtoul(nexttok(&ptr), 0, 10); // kstkeip   
  85.     nexttok(&ptr); // signal   
  86.     nexttok(&ptr); // blocked   
  87.     nexttok(&ptr); // sigignore   
  88.     nexttok(&ptr); // sigcatch   
  89.     wchan = strtoul(nexttok(&ptr), 0, 10); // wchan   
  90.     nexttok(&ptr); // nswap   
  91.     nexttok(&ptr); // cnswap   
  92.     nexttok(&ptr); // exit signal   
  93.     nexttok(&ptr); // processor   
  94.     rtprio = atoi(nexttok(&ptr)); // rt_priority   
  95.     sched = atoi(nexttok(&ptr)); // scheduling policy   
  96.       
  97.     tty = atoi(nexttok(&ptr));  
  98.       
  99.     if(tid != 0) {  
  100.         ppid = pid;  
  101.         pid = tid;  
  102.     }  
  103.   
  104.     pw = getpwuid(stats.st_uid);  
  105.     if(pw == 0) {  
  106.         sprintf(user,"%d",(int)stats.st_uid);  
  107.     } else {  
  108.         strcpy(user,pw->pw_name);  
  109.     }  
  110.       
  111.     if(!namefilter || !strncmp(name, namefilter, strlen(namefilter))) {  
  112.         printf("%-8s %-5d %-5d %-5d %-5d", user, pid, ppid, vss / 1024, rss * 4);  
  113.         if(display_flags&SHOW_PRIO)  
  114.             printf(" %-5d %-5d %-5d %-5d", prio, nice, rtprio, sched);  
  115.         printf(" %08x %08x %s %s", wchan, eip, state, cmdline[0] ? cmdline : name);  
  116.         if(display_flags&SHOW_TIME)  
  117.             printf(" (u:%d, s:%d)", utime, stime);  
  118.         printf("\n");  
  119.     }  
  120.     return 0;  
  121. }  
static int ps_line(int pid, int tid, char *namefilter)
{
    char statline[1024];
    char cmdline[1024];
    char user[32];
    struct stat stats;
    int fd, r;
    char *ptr, *name, *state;
    int ppid, tty;
    unsigned wchan, rss, vss, eip;
    unsigned utime, stime;
    int prio, nice, rtprio, sched;
    struct passwd *pw;
    
    sprintf(statline, "/proc/%d", pid);
    stat(statline, &stats);

    if(tid) {
        sprintf(statline, "/proc/%d/task/%d/stat", pid, tid);
        cmdline[0] = 0;
    } else {
        sprintf(statline, "/proc/%d/stat", pid);
        sprintf(cmdline, "/proc/%d/cmdline", pid);    
        fd = open(cmdline, O_RDONLY);
        if(fd == 0) {
            r = 0;
        } else {
            r = read(fd, cmdline, 1023);
            close(fd);
            if(r < 0) r = 0;
        }
        cmdline[r] = 0;
    }
    
    fd = open(statline, O_RDONLY);
    if(fd == 0) return -1;
    r = read(fd, statline, 1023);
    close(fd);
    if(r < 0) return -1;
    statline[r] = 0;

    ptr = statline;
    nexttok(&ptr); // skip pid
    ptr++;          // 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); // pgrp
    nexttok(&ptr); // sid
    tty = atoi(nexttok(&ptr));
    
    nexttok(&ptr); // tpgid
    nexttok(&ptr); // flags
    nexttok(&ptr); // minflt
    nexttok(&ptr); // cminflt
    nexttok(&ptr); // majflt
    nexttok(&ptr); // cmajflt
#if 1
    utime = atoi(nexttok(&ptr));
    stime = atoi(nexttok(&ptr));
#else
    nexttok(&ptr); // utime
    nexttok(&ptr); // stime
#endif
    nexttok(&ptr); // cutime
    nexttok(&ptr); // cstime
    prio = atoi(nexttok(&ptr));
    nice = atoi(nexttok(&ptr));
    nexttok(&ptr); // threads
    nexttok(&ptr); // itrealvalue
    nexttok(&ptr); // starttime
    vss = strtoul(nexttok(&ptr), 0, 10); // vsize
    rss = strtoul(nexttok(&ptr), 0, 10); // rss
    nexttok(&ptr); // rlim
    nexttok(&ptr); // startcode
    nexttok(&ptr); // endcode
    nexttok(&ptr); // startstack
    nexttok(&ptr); // kstkesp
    eip = strtoul(nexttok(&ptr), 0, 10); // kstkeip
    nexttok(&ptr); // signal
    nexttok(&ptr); // blocked
    nexttok(&ptr); // sigignore
    nexttok(&ptr); // sigcatch
    wchan = strtoul(nexttok(&ptr), 0, 10); // wchan
    nexttok(&ptr); // nswap
    nexttok(&ptr); // cnswap
    nexttok(&ptr); // exit signal
    nexttok(&ptr); // processor
    rtprio = atoi(nexttok(&ptr)); // rt_priority
    sched = atoi(nexttok(&ptr)); // scheduling policy
    
    tty = atoi(nexttok(&ptr));
    
    if(tid != 0) {
        ppid = pid;
        pid = tid;
    }

    pw = getpwuid(stats.st_uid);
    if(pw == 0) {
        sprintf(user,"%d",(int)stats.st_uid);
    } else {
        strcpy(user,pw->pw_name);
    }
    
    if(!namefilter || !strncmp(name, namefilter, strlen(namefilter))) {
        printf("%-8s %-5d %-5d %-5d %-5d", user, pid, ppid, vss / 1024, rss * 4);
        if(display_flags&SHOW_PRIO)
            printf(" %-5d %-5d %-5d %-5d", prio, nice, rtprio, sched);
        printf(" %08x %08x %s %s", wchan, eip, state, cmdline[0] ? cmdline : name);
        if(display_flags&SHOW_TIME)
            printf(" (u:%d, s:%d)", utime, stime);
        printf("\n");
    }
    return 0;
}



 



方法2: 进入/proc文件夹

在/proc文件夹下有很多对应进程ID号的子文件夹:

  1. drwxr-xr-x  23 root       root             4096 2012-10-01 02:09 ../  
  2. dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 1/  
  3. dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 10/  
  4. dr-xr-xr-x   7 root       root                0 2013-01-25 21:11 1001/  
  5. dr-xr-xr-x   7 root       root                0 2013-01-25 21:11 1076/  
  6. dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 11/  
  7. dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 12/  
  8. dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 13/  
  9. dr-xr-xr-x   7 gdm        gdm                 0 2013-01-25 21:11 1345/  
  10. dr-xr-xr-x   7 root       root                0 2013-01-25 21:11 1353/  
  11. dr-xr-xr-x   7 root       root                0 2013-01-25 21:11 1375/  
  12. dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 14/  
drwxr-xr-x  23 root       root             4096 2012-10-01 02:09 ../
dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 1/
dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 10/
dr-xr-xr-x   7 root       root                0 2013-01-25 21:11 1001/
dr-xr-xr-x   7 root       root                0 2013-01-25 21:11 1076/
dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 11/
dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 12/
dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 13/
dr-xr-xr-x   7 gdm        gdm                 0 2013-01-25 21:11 1345/
dr-xr-xr-x   7 root       root                0 2013-01-25 21:11 1353/
dr-xr-xr-x   7 root       root                0 2013-01-25 21:11 1375/
dr-xr-xr-x   7 root       root                0 2013-01-25 21:10 14/


 

我们可以进入对应的文件夹内,可以看到有以下信息,就可以查询到你的进程信息了。


(作者:LL   出处:http://blog.csdn.net/tcpipstack , 欢迎转载,也请保留这段声明。谢谢!)


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值