Browse Source

3.13 commit

master
陈越 3 years ago
parent
commit
a1e8eb5a8b
6 changed files with 497 additions and 23 deletions
  1. +4
    -5
      fun.h
  2. +8
    -9
      fun_1.c
  3. +78
    -8
      fun_2.c
  4. BIN
      main
  5. +0
    -1
      main.c
  6. +407
    -0
      mytop.c

+ 4
- 5
fun.h View File

@ -5,11 +5,10 @@
#include "fun_1.c"
#include "fun_2.c"
//void history(char *cmdline);
//void print_his(char *argv);
void history(char *cmdline);
void print_his(char *argv);
void eval(char *cmdline);
int parseline(const char *cmdline, char **argv);
//void eval(char *cmdline);
//int parseline(const char *cmdline, char **argv);
#endif // FUN_H_INCLUDED

+ 8
- 9
fun_1.c View File

@ -1,9 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wait.h>
#include <signal.h>
#include<fcntl.h>
#include "mytop.c"
//#include "fun.h"
@ -26,6 +27,9 @@ void eval(char *cmdline)
{
if(isBg)
{
int fd = open( "/dev/null", O_RDWR );
dup2( fd, 0 );
dup2( fd, 1 );
signal(SIGCHLD, SIG_IGN);
}
if(execvp(argv[0],argv)<0)
@ -109,7 +113,7 @@ int builtin_cmd(char **argv)
{
return 1;
}
if(Is_open(argv)==1)
if(Is_redirect(argv)==1)
{
return 1;
}
@ -119,7 +123,7 @@ int builtin_cmd(char **argv)
}
if(!strcmp(argv[0],"history"))
{
print_his(argv[1]);
print_history(argv[1]);
return 1;
}
if(!strcmp(argv[0],"cd"))
@ -137,15 +141,10 @@ int builtin_cmd(char **argv)
return 1;
}
}
if(!strcmp(argv[0],"mytop"))
{
mytop();
return 1;
}
if(!strcmp(argv[0],"bg")||!strcmp(argv[0],"fg"))
{
return 1;
}
return 0;
}

+ 78
- 8
fun_2.c View File

@ -11,7 +11,7 @@ void history(char *cmdline)
num++;
}
void print_his(char *argv)
void print_history(char *argv)
{
int i = num - atoi(argv);
@ -96,7 +96,7 @@ void shell_pip(char **prog1, char **prog2)
return 0;
}
int Is_open(char **argv)
int Is_redirect(char **argv)
{
int i = 0;
int Isopen = 0;
@ -129,15 +129,16 @@ int Is_open(char **argv)
i++;
j++;
}
cmd_1[j] = NULL;
shell_open(cmd_0, cmd_1);
cmd_1[j] = NULL;
shell_direct(cmd_0, cmd_1);
return 1;
}
void shell_open(char **prog1, char **prog2)
void shell_direct(char **prog1, char **prog2)
{
///
int fd;
if(prog1)
if(fork() == 0)
{
if( (fd=open(prog2[0],O_RDWR | O_CREAT | O_NOCTTY | O_NDELAY))<0 )
@ -149,8 +150,77 @@ void shell_open(char **prog1, char **prog2)
dup(fd);
close(fd);
execvp(prog1[0], prog1);
exit(1);
remove(prog2[0]);
exit(0);
}
wait(0);
if(fork() == 0)
{
if( (fd=open(prog1[0],O_RDWR | O_NOCTTY | O_NDELAY))<0 )
{
exit(0);
}
close(0);
dup(fd);
close(fd);
execvp(prog2[0], prog2);
exit(0);
}
wait(0);
}
}
/*
void mytop()
{
FILE *fp;
unsigned int pagesize;
unsigned long total, free, largest, cached;
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);
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)
{
int i;
u64_t t = 0;
//proc的滴答proc和当前进程prev_proc做比较endpoint相等
for(i = 0; i < CPUTIMENAMES; i++) {
if(!CPUTIME(timemode, i))
continue;
if(p1->p_endpoint == p2->p_endpoint) {
t = t + p2->p_cpucycles[i] - p1->p_cpucycles[i];
} else {
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;
}
*/

BIN
main View File


+ 0
- 1
main.c View File

@ -15,6 +15,5 @@ int main()
eval(cmdline);
history(cmdline);
}
return 0;
}

+ 407
- 0
mytop.c View File

@ -0,0 +1,407 @@
/*
#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>
#include <termios.h>
#include <time.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <assert.h>
typedef int endpoint_t;
typedef uint64_t u64_t;
typedef long unsigned int vir_bytes;
#define USED 0x1
#define IS_TASK 0x2
#define IS_SYSTEM 0x4
#define BLOCKED 0x8
#define TYPE_TASK 'T'
#define TYPE_SYSTEM 'S'
#define STATE_RUN 'R'
#define MAX_NR_TASKS 1023
#define SELF ((endpoint_t) 0x8ace)
#define _MAX_MAGIC_PROC (SELF)
#define _ENDPOINT_GENERATION_SIZE (MAX_NR_TASKS+_MAX_MAGIC_PROC+1)
#define _ENDPOINT_P(e) \
((((e)+MAX_NR_TASKS) % _ENDPOINT_GENERATION_SIZE) - MAX_NR_TASKS)
#define SLOT_NR(e) (_ENDPOINT_P(e) + 5)
#define _PATH_PROC "/proc"
#define CPUTIME(m, i) (m & (1L << (i)))
const char *cputimenames[] = { "user", "ipc", "kernelcall" };
#define CPUTIMENAMES (sizeof(cputimenames)/sizeof(cputimenames[0]))
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;
pid_t p_pid;
u64_t p_cpucycles[CPUTIMENAMES];
int p_priority;
endpoint_t p_blocked;
time_t p_user_time;
vir_bytes p_memory;
uid_t p_effuid;
int p_nice;
char p_name[16+1];
};
struct proc *proc = NULL, *prev_proc = NULL;
//u64_t 64 high和low32位 64 high+low
static inline u64_t make64(unsigned long lo, unsigned long hi)
{
return ((u64_t)hi << 32) | (u64_t)lo;
}
//pid/psinfo的信息读出来
//
void parse_file(pid_t pid)
{
char path[PATH_MAX], name[256], type, state;
int version, endpt, effuid;
unsigned long cycles_hi, cycles_lo;
FILE *fp;
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;
}
//file数
//filenum+=1;
//slot超出了nr_total
slot = SLOT_NR(endpt);
slot++;
//slot=slot_a;
//slot_a+=1;//
//endpoint的值是否合理 0nr_total的范围内
if(slot < 0 || slot >= nr_total) {
fprintf(stderr, "top: unreasonable endpoint number %d\n", endpt);
fclose(fp);
return;
}
//slot为该进程结构体在数组中的位置
p = &proc[slot];//slot地址赋值给p
if (type == TYPE_TASK)
//task进程
p->p_flags |= IS_TASK;
else if (type == TYPE_SYSTEM)
//system进程
p->p_flags |= IS_SYSTEM;
//endpt和pid存入对应进程结构体
p->p_endpoint = endpt;
p->p_pid = pid;
//
if (fscanf(fp, " %255s %c %d %d %lu %*u %lu %lu",
name, &state, &p->p_blocked, &p->p_priority,
&p->p_user_time, &cycles_hi, &cycles_lo) != 7) {
fclose(fp);
return;
}
//
strncpy(p->p_name, name, sizeof(p->p_name)-1);
//0
p->p_name[sizeof(p->p_name)-1] = 0;
if (state != STATE_RUN)//run的进程
p->p_flags |= BLOCKED;//
//64p_cpucycles[]
p->p_cpucycles[0] = make64(cycles_lo, cycles_hi);
p->p_memory = 0L;
//ID
if (!(p->p_flags & IS_TASK)) {
int j;
// ID
if ((j=fscanf(fp, " %lu %*u %*u %*c %*d %*u %u %*u %d %*c %*d %*u",
&p->p_memory, &effuid, &p->p_nice)) != 3) {
fclose(fp);
return;
}
p->p_effuid = effuid;
} else p->p_effuid = 0;
//CPUTIMENAMES次cycles_hi,cycle_lo
for(i = 1; i < CPUTIMENAMES; i++) {
if(fscanf(fp, " %lu %lu",
&cycles_hi, &cycles_lo) == 2) {
//64p_cpucycles[]
p->p_cpucycles[i] = make64(cycles_lo, cycles_hi);
} else {
p->p_cpucycles[i] = 0;
}
}
//
if ((p->p_flags & IS_TASK)) {
if(fscanf(fp, " %lu", &p->p_memory) != 1) {
p->p_memory = 0;
}
}
//
p->p_flags |= USED;
fclose(fp);
}
void parse_dir(void)
{
DIR *p_dir;
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)
{
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)
{
int i;
u64_t t = 0;
//proc的滴答proc和当前进程prev_proc做比较endpoint相等
for(i = 0; i < CPUTIMENAMES; i++) {
if(!CPUTIME(timemode, i))
continue;
if(p1->p_endpoint == p2->p_endpoint) {
t = t + p2->p_cpucycles[i] - p1->p_cpucycles[i];
} else {
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)
{
int p, nprocs;
u64_t idleticks = 0;
u64_t kernelticks = 0;
u64_t systemticks = 0;
u64_t userticks = 0;
u64_t total_ticks = 0;
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);
}
}
for(p = nprocs = 0; p < nr_total; p++) {
u64_t uticks;
//used就continue
if(!(proc2[p].p_flags & USED))
continue;
tick_procs[nprocs].p = proc2 + p;
tick_procs[nprocs].ticks = cputicks(&proc1[p], &proc2[p], cputimemode);
//uticks
uticks = cputicks(&proc1[p], &proc2[p], 1);
//ticks
total_ticks = total_ticks + uticks;
//idletick
//0continue
if(p-5 == 317) {
idleticks = uticks;
continue;
}
//kerneltick
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;
else
userticks = userticks + tick_procs[nprocs].ticks;
}
nprocs++;
}
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();
}
void getkinfo(void)
{
FILE *fp;
if ((fp = fopen("/proc/kinfo", "r")) == NULL) {
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
if (chdir("/proc") != 0) {
perror("chdir to /proc" );
return 1;
}
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()
{
}

Loading…
Cancel
Save