diff --git a/main b/main deleted file mode 100755 index 681cab6..0000000 Binary files a/main and /dev/null differ diff --git a/main.c b/main.c index f4c5b9e..492057a 100644 --- a/main.c +++ b/main.c @@ -4,7 +4,6 @@ pid_t main_proc_pid; void sigint_handler() { - ff(stdin) pid_t pid = getpid(); if(pid != main_proc_pid) { @@ -20,8 +19,8 @@ int main() while(1) { - ff(stdin); printf("COMMAND->"); + fflush(stdin); fgets(cmdline, MAXLINE, stdin); eval(cmdline); history(cmdline); diff --git a/mytop.c b/mytop.c index 9712959..c63ec7f 100644 --- a/mytop.c +++ b/mytop.c @@ -1,10 +1,9 @@ -/* -#define mytop_h + +//#define mytop_h #include #include #include #include -//#include #include #include #include @@ -44,10 +43,7 @@ const char *cputimenames[] = { "user", "ipc", "kernelcall" }; unsigned int nr_procs, nr_tasks; int nr_total=0; -//int slot_a=0; -//int pronum=0; -//int filenum=0; -//proc 结构体 + struct proc { int p_flags; endpoint_t p_endpoint; @@ -79,22 +75,17 @@ void parse_file(pid_t pid) struct proc *p; int slot; int i; - sprintf(path, "/proc/%d/psinfo", pid); - //按照/proc/%d/psinfo打开path中的文件 if ((fp = fopen(path, "r")) == NULL) return; - //version是否为1,如果不是该进程不需要记录 if (fscanf(fp, "%d", &version) != 1) { fclose(fp); return; } - //versions错误处理 if (version != 0) { fputs("procfs version mismatch!\n", stderr); exit(1); } - //读入类型和端点 判断是否读入的是两个 if (fscanf(fp, " %c %d", &type, &endpt) != 2) { fclose(fp); return; @@ -174,8 +165,6 @@ void parse_file(pid_t pid) } //按位或 p->p_flags |= USED; - - fclose(fp); } void parse_dir(void) @@ -184,27 +173,18 @@ void parse_dir(void) struct dirent *p_ent; pid_t pid; char *end; - //打开/proc if ((p_dir = opendir("/proc/")) == NULL) { perror("opendir on /proc"); exit(1); } - //readdir()返回参数p_dir 目录流的下个目录进入点。 p_ent=readdir(p_dir); while(p_ent != NULL){ -// if(strncpy(p_ent->d_name,"/proc",1)==0) -// continue; - //分析出里面所有pid pid=strtol(p_ent->d_name,&end,10); if(pid!=0 && !end[0]){ - //printf("%l\n",pid); - //一个pid调用一次parse_file parse_file(pid); } - p_ent=readdir(p_dir); } - closedir(p_dir); } int print_memory(void) @@ -212,33 +192,26 @@ int print_memory(void) FILE *fp; unsigned int pagesize; unsigned long total, free, largest, cached; - //打开meminfo if ((fp = fopen("/proc/meminfo", "r")) == NULL) return 0; - //读输入 if (fscanf(fp, "%u %lu %lu %lu %lu", &pagesize, &total, &free, &largest, &cached) != 5) { fclose(fp); return 0; } - fclose(fp); - //打印memory信息 printf("main memory: %ldK total, %ldK free, %ldK contig free, " "%ldK cached\n", (pagesize * total)/1024, (pagesize * free)/1024, (pagesize * largest)/1024, (pagesize * cached)/1024); - return 1; } -//tp 结构体 -//包含了进程指针p和ticks,对应某个进程和滴答 + struct tp { struct proc *p; u64_t ticks; }; //计算cputicks 用到当前进程和其他进程的,还涉及CPUTIME - //滴答并不是简单的结构体中的滴答,因为在写文件的时候需要更新。需要通过当前进程来和该进程一起计算 u64_t cputicks(struct proc *p1, struct proc *p2, int timemode) { @@ -254,17 +227,9 @@ u64_t cputicks(struct proc *p1, struct proc *p2, int timemode) t = t + p2->p_cpucycles[i]; } } -// for(i = 0; i < CPUTIMENAMES; i++) { -// if(!CPUTIME(timemode, i)) -// continue; -// if(proc->p_endpoint == prev_proc->p_endpoint) { -// t = t + prev_proc->p_cpucycles[i] - proc->p_cpucycles[i]; -// } else { -// t = t + prev_proc->p_cpucycles[i]; -// } -// } return t; } + void print_procs( struct proc *proc1, struct proc *proc2, int cputimemode) { @@ -277,10 +242,7 @@ void print_procs( int blockedseen = 0; static struct tp *tick_procs = NULL; if (tick_procs == NULL) { - //给tick_procs分配内存 - //创建tp结构体tick_procs tick_procs = malloc(nr_total * sizeof(tick_procs[0])); - //tick procs错误处理 if (tick_procs == NULL) { fprintf(stderr, "Out of memory!\n"); exit(1); @@ -308,13 +270,6 @@ void print_procs( if(p-5 == ((endpoint_t) -1)) { kernelticks = uticks; } -// if(!(proc2[p].p_flags & IS_TASK)) { -// if(proc2[p].p_flags & IS_SYSTEM) -// systemticks = systemticks + tick_procs[nprocs].ticks; -// else -// userticks = userticks + tick_procs[nprocs].ticks; -// } - //判断是否为systemtick和usertick if(!(proc2[p].p_flags & IS_TASK)) { if(proc2[p].p_flags & IS_SYSTEM) systemticks = systemticks + tick_procs[nprocs].ticks; @@ -327,40 +282,31 @@ void print_procs( if (total_ticks == 0) return; - //打印user system kernel idle的情况 printf("CPU states: %6.2f%% user, ", 100.0 * userticks / total_ticks); printf("%6.2f%% system, ", 100.0 * systemticks / total_ticks); printf("%6.2f%% kernel, ", 100.0 * kernelticks/ total_ticks); printf("%6.2f%% idle",100.00-(100.0 * (kernelticks+userticks+systemticks)/ total_ticks)); printf("\n"); } - //get_procs将所有需要的信息放在结构体数组proc[]中,每个元素都是一个进程结构体。 + void get_procs(void) { struct proc *p; int i; - p = prev_proc; - //记录当前进程,赋值给prev_proc prev_proc = proc; proc = p; if (proc == NULL) { - //分配内存 - //每个进程分配一个结构体 - //分配nr_total个单位proc结构体内存空间,并把指针赋予proc proc = malloc(nr_total * sizeof(proc[0])); - //错误处理 if (proc == NULL) { fprintf(stderr, "Out of memory!\n"); exit(1); } } - //先将所有flag置0 for (i = 0; i < nr_total; i++) proc[i].p_flags = 0; - //调用parse_dir分析pid parse_dir(); } @@ -372,18 +318,15 @@ void getkinfo(void) exit(1); } - //读如nr_procs,nr_tasks if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2) { exit(1); } - fclose(fp); - //算出总的nr_total nr_total = (int) (nr_procs + nr_tasks); } -int mytop(){ - //跳转到/proc +void mytop() +{ if (chdir("/proc") != 0) { perror("chdir to /proc" ); return 1; @@ -391,14 +334,8 @@ int mytop(){ print_memory(); getkinfo(); get_procs(); - //当前进程为空的话 就要再调用get_procs if(prev_proc==NULL) get_procs(); - print_procs(prev_proc,proc,1); - //fflush(NULL); - return 0; -} -*/ -int mytop(){} \ No newline at end of file +} \ No newline at end of file