|
|
我的ps,功能全,但是没有什么技术含量,完全是通过读取/proc/***/stat文件来实现的,由于感觉屏幕的尺寸有限在打印的时候只打印了部分信息,你可以仅仅修改printf语句就可以得到自己想要的输出,当然你也还可以通过命令参数来得到不同的输出。这些工作我都没有做。
- /*author:Arthur Echo(Tianjin University CS Xie Shuiquan)
- email:Arthur.Echo@gmail.com [email]anuode@gmail.com[/email]
- This program is very simply--just for study.You can
- use it freely.
- 2006.4.26
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <errno.h>
- typedef struct
- {
- pid_t pid;
- char comm[32];
- char state;
- pid_t ppid;
- int pgrp;
- int session;
- int tty_nr;
- int tpgid;
- unsigned long flags;
- unsigned long minflt;
- unsigned long cminflt;
- unsigned long majflt;
- unsigned long cmajflt;
- unsigned long utime;
- unsigned long stime;
- long cutime;
- long cstime;
- long priority;
- long nice;
- long zero;
- long itrealvalue;
- unsigned long starttime;
- unsigned long vsize;
- long rss;
- unsigned long rlim;
- unsigned long startcode;
- unsigned long endcode;
- unsigned long startstack;
- unsigned long kstkesp;
- unsigned long kstkeip;
- unsigned long signal;
- unsigned long blocked;
- unsigned long sigignore;
- unsigned long sigcatch;
- unsigned long wchan;
- unsigned long nswap;
- unsigned long cnswap;
- int exit_signal;
- int processor;
- }proc_info_t;
-
- int get_proc_info(char * buf,proc_info_t * info,int pid_length)
- {
- char * tmp;
- char tmp_pid[5];
- strncpy(tmp_pid,buf,pid_length);
- tmp_pid[pid_length] = '\0';
- info->pid = atoi(tmp_pid);
- int length = 0;
- for(;buf[pid_length + 2 + length] != ')';length++);
- strncpy(info->comm,buf + pid_length + 2,length);
- info->comm[length] = '\0';
- info->state = buf[pid_length + length + 4];
-
- tmp = &buf[pid_length + length +6];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->ppid = atoi(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->pgrp = atoi(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->session = atoi(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->tty_nr = atoi(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->tpgid = atoi(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->flags = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->minflt = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->cminflt = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->majflt = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->cmajflt = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->utime = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->stime = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->cutime = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->cstime = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->priority = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->nice = atol(tmp_pid);
- info->zero = 0;
- tmp = &tmp[length +3];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->itrealvalue = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->starttime = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->vsize = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->rss = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->rlim = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->startcode = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->endcode = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->startstack = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->kstkesp = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->kstkeip = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->signal = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->blocked = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->sigignore = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->sigcatch = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->wchan = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->nswap = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->cnswap = atol(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->exit_signal = atoi(tmp_pid);
- tmp = &tmp[length +1];
- for(length = 0;tmp[length] != 32;length++);
- strncpy(tmp_pid,tmp,length);
- tmp_pid[length] = '\0';
- info->processor = atoi(tmp_pid);
- return 0;
- }
- int main(int argc,char * argv[])
- {
- int fd;
- proc_info_t proc_info;
- char proc_info_buf[256];
- DIR *dir;
- char path[20];
- dir = opendir("/proc");
- struct dirent *dp;
- printf("\033[H\033[J");
- printf("%5s %16s %5s %5s %5s %5s %5s %5s %5s %5s\n\n","PID","Command","State","PPID","PGrp","Session","tty_nr","TpgID","Prior","Nice");
- while((dp = readdir(dir)) != NULL)
- {
- if(dp->d_name[0] < 58 && dp->d_name[0] > 48)
- {
- strcpy(path,"/proc/");
- strcat(path,dp->d_name);
- strcat(path,"/stat");
- fd = open(path,O_RDONLY);
- read(fd,proc_info_buf,256);
- get_proc_info(proc_info_buf,&proc_info,strlen(dp->d_name));
- 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);
- close(fd);
- }
- }
- closedir(dir);
- exit(0);
- }
复制代码 |
|