陈越 hace 3 años
padre
commit
447ac9fe67
Se han modificado 3 ficheros con 10 adiciones y 74 borrados
  1. BIN
      main
  2. +1
    -2
      main.c
  3. +9
    -72
      mytop.c

BIN
main Ver fichero


+ 1
- 2
main.c Ver fichero

@ -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);

+ 9
- 72
mytop.c Ver fichero

@ -1,10 +1,9 @@
/*
#define mytop_h
//#define mytop_h
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <curses.h>
//#include <timers.h>
#include <stdlib.h>
#include <limits.h>
#include <termcap.h>
@ -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(){}
}

Cargando…
Cancelar
Guardar