LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 726|回复: 0

Arthur Echo's ps

[复制链接]
发表于 2006-4-26 17:58:51 | 显示全部楼层 |阅读模式
我的ps,功能全,但是没有什么技术含量,完全是通过读取/proc/***/stat文件来实现的,由于感觉屏幕的尺寸有限在打印的时候只打印了部分信息,你可以仅仅修改printf语句就可以得到自己想要的输出,当然你也还可以通过命令参数来得到不同的输出。这些工作我都没有做。

  1. /*author:Arthur Echo(Tianjin University CS Xie Shuiquan)
  2.   email:Arthur.Echo@gmail.com [email]anuode@gmail.com[/email]

  3.   This program is very simply--just for study.You can
  4.   use it freely.

  5.   2006.4.26
  6. */
  7.                

  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <dirent.h>
  11. #include <fcntl.h>
  12. #include <sys/stat.h>
  13. #include <errno.h>


  14. typedef struct
  15. {
  16.         pid_t pid;
  17.         char comm[32];
  18.         char state;
  19.         pid_t ppid;
  20.         int pgrp;
  21.         int session;
  22.         int tty_nr;
  23.         int tpgid;
  24.         unsigned long flags;
  25.         unsigned long minflt;
  26.         unsigned long cminflt;
  27.         unsigned long majflt;
  28.         unsigned long cmajflt;
  29.         unsigned long utime;
  30.         unsigned long stime;
  31.         long cutime;
  32.         long cstime;
  33.         long priority;
  34.         long nice;
  35.         long zero;
  36.         long itrealvalue;
  37.         unsigned long starttime;
  38.         unsigned long vsize;
  39.         long rss;
  40.         unsigned long rlim;
  41.         unsigned long startcode;
  42.         unsigned long endcode;
  43.         unsigned long startstack;
  44.         unsigned long kstkesp;
  45.         unsigned long kstkeip;
  46.         unsigned long signal;
  47.         unsigned long blocked;
  48.         unsigned long sigignore;
  49.         unsigned long sigcatch;
  50.         unsigned long wchan;
  51.         unsigned long nswap;
  52.         unsigned long cnswap;
  53.         int exit_signal;
  54.         int processor;
  55. }proc_info_t;
  56.        
  57. int get_proc_info(char * buf,proc_info_t * info,int pid_length)
  58. {
  59.         char * tmp;
  60.         char tmp_pid[5];

  61.         strncpy(tmp_pid,buf,pid_length);
  62.         tmp_pid[pid_length] = '\0';
  63.         info->pid = atoi(tmp_pid);

  64.         int length = 0;
  65.         for(;buf[pid_length + 2 + length] != ')';length++);
  66.         strncpy(info->comm,buf + pid_length + 2,length);
  67.         info->comm[length] = '\0';

  68.         info->state = buf[pid_length + length + 4];
  69.        

  70.         tmp = &buf[pid_length + length +6];
  71.         for(length = 0;tmp[length] != 32;length++);
  72.         strncpy(tmp_pid,tmp,length);
  73.         tmp_pid[length] = '\0';
  74.         info->ppid = atoi(tmp_pid);

  75.         tmp = &tmp[length +1];
  76.         for(length = 0;tmp[length] != 32;length++);
  77.         strncpy(tmp_pid,tmp,length);
  78.         tmp_pid[length] = '\0';
  79.         info->pgrp = atoi(tmp_pid);

  80.         tmp = &tmp[length +1];
  81.         for(length = 0;tmp[length] != 32;length++);
  82.         strncpy(tmp_pid,tmp,length);
  83.         tmp_pid[length] = '\0';
  84.         info->session = atoi(tmp_pid);

  85.         tmp = &tmp[length +1];
  86.         for(length = 0;tmp[length] != 32;length++);
  87.         strncpy(tmp_pid,tmp,length);
  88.         tmp_pid[length] = '\0';
  89.         info->tty_nr = atoi(tmp_pid);

  90.         tmp = &tmp[length +1];
  91.         for(length = 0;tmp[length] != 32;length++);
  92.         strncpy(tmp_pid,tmp,length);
  93.         tmp_pid[length] = '\0';
  94.         info->tpgid = atoi(tmp_pid);

  95.         tmp = &tmp[length +1];
  96.         for(length = 0;tmp[length] != 32;length++);
  97.         strncpy(tmp_pid,tmp,length);
  98.         tmp_pid[length] = '\0';
  99.         info->flags = atol(tmp_pid);

  100.         tmp = &tmp[length +1];
  101.         for(length = 0;tmp[length] != 32;length++);
  102.         strncpy(tmp_pid,tmp,length);
  103.         tmp_pid[length] = '\0';
  104.         info->minflt = atol(tmp_pid);

  105.         tmp = &tmp[length +1];
  106.         for(length = 0;tmp[length] != 32;length++);
  107.         strncpy(tmp_pid,tmp,length);
  108.         tmp_pid[length] = '\0';
  109.         info->cminflt = atol(tmp_pid);

  110.         tmp = &tmp[length +1];
  111.         for(length = 0;tmp[length] != 32;length++);
  112.         strncpy(tmp_pid,tmp,length);
  113.         tmp_pid[length] = '\0';
  114.         info->majflt = atol(tmp_pid);

  115.         tmp = &tmp[length +1];
  116.         for(length = 0;tmp[length] != 32;length++);
  117.         strncpy(tmp_pid,tmp,length);
  118.         tmp_pid[length] = '\0';
  119.         info->cmajflt = atol(tmp_pid);

  120.         tmp = &tmp[length +1];
  121.         for(length = 0;tmp[length] != 32;length++);
  122.         strncpy(tmp_pid,tmp,length);
  123.         tmp_pid[length] = '\0';
  124.         info->utime = atol(tmp_pid);

  125.         tmp = &tmp[length +1];
  126.         for(length = 0;tmp[length] != 32;length++);
  127.         strncpy(tmp_pid,tmp,length);
  128.         tmp_pid[length] = '\0';
  129.         info->stime = atol(tmp_pid);

  130.         tmp = &tmp[length +1];
  131.         for(length = 0;tmp[length] != 32;length++);
  132.         strncpy(tmp_pid,tmp,length);
  133.         tmp_pid[length] = '\0';
  134.         info->cutime = atol(tmp_pid);

  135.         tmp = &tmp[length +1];
  136.         for(length = 0;tmp[length] != 32;length++);
  137.         strncpy(tmp_pid,tmp,length);
  138.         tmp_pid[length] = '\0';
  139.         info->cstime = atol(tmp_pid);

  140.          tmp = &tmp[length +1];
  141.         for(length = 0;tmp[length] != 32;length++);
  142.         strncpy(tmp_pid,tmp,length);
  143.         tmp_pid[length] = '\0';
  144.         info->priority = atol(tmp_pid);

  145.         tmp = &tmp[length +1];
  146.         for(length = 0;tmp[length] != 32;length++);
  147.         strncpy(tmp_pid,tmp,length);
  148.         tmp_pid[length] = '\0';
  149.         info->nice = atol(tmp_pid);

  150.          info->zero = 0;

  151.          tmp = &tmp[length +3];
  152.         for(length = 0;tmp[length] != 32;length++);
  153.         strncpy(tmp_pid,tmp,length);
  154.         tmp_pid[length] = '\0';
  155.         info->itrealvalue = atol(tmp_pid);

  156.          tmp = &tmp[length +1];
  157.         for(length = 0;tmp[length] != 32;length++);
  158.         strncpy(tmp_pid,tmp,length);
  159.         tmp_pid[length] = '\0';
  160.         info->starttime = atol(tmp_pid);

  161.          tmp = &tmp[length +1];
  162.         for(length = 0;tmp[length] != 32;length++);
  163.         strncpy(tmp_pid,tmp,length);
  164.         tmp_pid[length] = '\0';
  165.         info->vsize = atol(tmp_pid);

  166.             tmp = &tmp[length +1];
  167.         for(length = 0;tmp[length] != 32;length++);
  168.         strncpy(tmp_pid,tmp,length);
  169.         tmp_pid[length] = '\0';
  170.         info->rss = atol(tmp_pid);

  171.          tmp = &tmp[length +1];
  172.         for(length = 0;tmp[length] != 32;length++);
  173.         strncpy(tmp_pid,tmp,length);
  174.         tmp_pid[length] = '\0';
  175.         info->rlim = atol(tmp_pid);

  176.          tmp = &tmp[length +1];
  177.         for(length = 0;tmp[length] != 32;length++);
  178.         strncpy(tmp_pid,tmp,length);
  179.         tmp_pid[length] = '\0';
  180.         info->startcode = atol(tmp_pid);

  181.          tmp = &tmp[length +1];
  182.         for(length = 0;tmp[length] != 32;length++);
  183.         strncpy(tmp_pid,tmp,length);
  184.         tmp_pid[length] = '\0';
  185.         info->endcode = atol(tmp_pid);

  186.          tmp = &tmp[length +1];
  187.         for(length = 0;tmp[length] != 32;length++);
  188.         strncpy(tmp_pid,tmp,length);
  189.         tmp_pid[length] = '\0';
  190.         info->startstack = atol(tmp_pid);

  191.         tmp = &tmp[length +1];
  192.         for(length = 0;tmp[length] != 32;length++);
  193.         strncpy(tmp_pid,tmp,length);
  194.         tmp_pid[length] = '\0';
  195.         info->kstkesp = atol(tmp_pid);

  196.          tmp = &tmp[length +1];
  197.         for(length = 0;tmp[length] != 32;length++);
  198.         strncpy(tmp_pid,tmp,length);
  199.         tmp_pid[length] = '\0';
  200.         info->kstkeip = atol(tmp_pid);

  201.         tmp = &tmp[length +1];
  202.         for(length = 0;tmp[length] != 32;length++);
  203.         strncpy(tmp_pid,tmp,length);
  204.         tmp_pid[length] = '\0';
  205.         info->signal = atol(tmp_pid);

  206.         tmp = &tmp[length +1];
  207.         for(length = 0;tmp[length] != 32;length++);
  208.         strncpy(tmp_pid,tmp,length);
  209.         tmp_pid[length] = '\0';
  210.         info->blocked = atol(tmp_pid);

  211.          tmp = &tmp[length +1];
  212.         for(length = 0;tmp[length] != 32;length++);
  213.         strncpy(tmp_pid,tmp,length);
  214.         tmp_pid[length] = '\0';
  215.         info->sigignore = atol(tmp_pid);

  216.         tmp = &tmp[length +1];
  217.         for(length = 0;tmp[length] != 32;length++);
  218.         strncpy(tmp_pid,tmp,length);
  219.         tmp_pid[length] = '\0';
  220.         info->sigcatch = atol(tmp_pid);

  221.         tmp = &tmp[length +1];
  222.         for(length = 0;tmp[length] != 32;length++);
  223.         strncpy(tmp_pid,tmp,length);
  224.         tmp_pid[length] = '\0';
  225.         info->wchan = atol(tmp_pid);

  226.          tmp = &tmp[length +1];
  227.         for(length = 0;tmp[length] != 32;length++);
  228.         strncpy(tmp_pid,tmp,length);
  229.         tmp_pid[length] = '\0';
  230.         info->nswap = atol(tmp_pid);

  231.         tmp = &tmp[length +1];
  232.         for(length = 0;tmp[length] != 32;length++);
  233.         strncpy(tmp_pid,tmp,length);
  234.         tmp_pid[length] = '\0';
  235.         info->cnswap = atol(tmp_pid);

  236.          tmp = &tmp[length +1];
  237.         for(length = 0;tmp[length] != 32;length++);
  238.         strncpy(tmp_pid,tmp,length);
  239.         tmp_pid[length] = '\0';
  240.         info->exit_signal = atoi(tmp_pid);

  241.         tmp = &tmp[length +1];
  242.         for(length = 0;tmp[length] != 32;length++);
  243.         strncpy(tmp_pid,tmp,length);
  244.         tmp_pid[length] = '\0';
  245.         info->processor = atoi(tmp_pid);

  246.          return 0;
  247. }

  248. int main(int argc,char * argv[])
  249. {
  250.         int fd;
  251.          proc_info_t proc_info;
  252.         char proc_info_buf[256];
  253.         DIR *dir;
  254.         char path[20];
  255.         dir = opendir("/proc");

  256.         struct dirent *dp;

  257.          printf("\033[H\033[J");
  258.         printf("%5s %16s %5s %5s %5s %5s %5s %5s %5s %5s\n\n","PID","Command","State","PPID","PGrp","Session","tty_nr","TpgID","Prior","Nice");

  259.         while((dp = readdir(dir)) != NULL)
  260.         {
  261.                 if(dp->d_name[0] < 58 && dp->d_name[0] > 48)
  262.                 {
  263.                         strcpy(path,"/proc/");
  264.                         strcat(path,dp->d_name);
  265.                         strcat(path,"/stat");

  266.                         fd = open(path,O_RDONLY);
  267.                         read(fd,proc_info_buf,256);
  268.                         get_proc_info(proc_info_buf,&proc_info,strlen(dp->d_name));
  269.                         printf("%5d %16s   %c   %5d %5d %5d %5d %5d %5lu %5lu\n",proc_info.pid,proc_info.comm,proc_info.state,proc_info.ppid,proc_info.pgrp,proc_info.session,proc_info.tty_nr,proc_info.tpgid,proc_info.priority,proc_info.nice);
  270.                         close(fd);
  271.                 }
  272.         }

  273.         closedir(dir);
  274.         exit(0);
  275. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表