Browse Source

3.16

master
陈越 3 years ago
parent
commit
678516033f
5 changed files with 869 additions and 381 deletions
  1. +2
    -0
      fun_1.c
  2. +32
    -29
      fun_2.c
  3. BIN
      main
  4. +180
    -0
      myselftop.c
  5. +655
    -352
      mytop.c

+ 2
- 0
fun_1.c View File

@ -27,6 +27,8 @@ void eval(char *cmdline)
{
if(isBg)
{
//close(1);
//close(0);
int fd = open( "/dev/null", O_RDWR );
dup2( fd, 0 );
dup2( fd, 1 );

+ 32
- 29
fun_2.c View File

@ -103,9 +103,11 @@ int Is_pipe(char **argv)
}
void shell_direct(char **prog1, char **prog2)
void shell_direct(char **prog1, char **prog2, char Isopen)
{
int fd;
if(Isopen == '>')
{
if(fork() == 0)
{
if( (fd=open(prog2[0],O_RDWR | O_CREAT | O_NOCTTY | O_NDELAY))<0 )
@ -135,27 +137,50 @@ void shell_direct(char **prog1, char **prog2)
}
wait(0);
}
if(Isopen == '<')
{
if(fork() == 0)
{
if( (fd=open(prog2[0],O_RDWR | O_NOCTTY | O_NDELAY))<0 )
{
perror("open");
exit(0);
}
close(0);
dup(fd);
close(fd);
execvp(prog1[0], prog1);
exit(0);
}
wait(0);
}
return;
}
int Is_redirect(char **argv)
{
int i = 0;
int Isopen = 0;
char Isopen = NULL;
char *cmd_0[MAXLINE];
char *cmd_1[MAXLINE];
while (argv[i]!=NULL)
{
if (argv[i][0] == '>')
{
Isopen = 1;
Isopen = '>';
}
if (argv[i][0] == '<')
{
Isopen = '<';
}
i++;
}
if(Isopen == 0)
if(Isopen == NULL)
{
return 0;
}
i = 0;
while (argv[i][0] != '>')
while (argv[i][0] != '>' && argv[i][0] != '<')
{
cmd_0[i] = argv[i];
i++;
@ -171,28 +196,6 @@ int Is_redirect(char **argv)
}
cmd_1[j] = NULL;
shell_direct(cmd_0, cmd_1);
shell_direct(cmd_0, cmd_1, Isopen);
return 1;
}
/*
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;
}
*/
}

BIN
main View File


+ 180
- 0
myselftop.c View File

@ -0,0 +1,180 @@
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <curses.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>
struct proc {
pid_t pid;
int tick;
};
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 proc *tick_procs = NULL;
tick_procs = malloc(nr_total * sizeof(struct proc));
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;
}
nprocs++;
}
if (total_ticks == 0)
return;
printf("CPU states: %6.2f%%", 100.0 * usedticks / total_ticks);
printf("\n");
}
int print_memory(void)
{
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;
}
int nr_procs, nr_tasks, nr_total;
void getkinfo(void)
{
FILE *fp;
if ((fp = fopen("/proc/kinfo", "r")) == NULL) {
exit(1);
}
if (fscanf(fp, "%u %u", &nr_procs, &nr_tasks) != 2) {
exit(1);
}
fclose(fp);
nr_total = (int) (nr_procs + nr_tasks);
}
void parse_psinfo(pid_t pid, struct proc pro)
{
char path[PATH_MAX];
char name[256], type, state;
int version, endpt, blocked, priority, tick;
FILE *fp;
sprintf(path, "/proc/%d/psinfo", pid);
if ((fp = fopen(path, "r")) == NULL)
return;
if (fscanf(fp, "%d %c %d %s %c %d %d", &version, &type, &endpt, name, &state, &blocked, &priority) != 7)
{
fclose(fp);
return;
}
//
if (fscanf(fp, "%d", &tick) < 0)
{
fclose(fp);
return;
}
//
pro.pid = pid;
pro.tick = tick;
fclose(fp);
}
void parse_dir(process)
{
DIR *p_dir;
struct dirent *p_ent;
pid_t pid;
char *end;
p_dir = opendir("/proc/")
//readdir(),/proc/pid/psinfo,/proc/PID
p_ent=readdir(p_dir);
int i=0;
while(p_ent != NULL)
{
pid=strtol(p_ent->d_name,&end,10);
if(pid!=0 && !end[0])
{
parse_psinfo(pid, process[i]);
i++;
}
p_ent=readdir(p_dir);
}
closedir(p_dir);
}
void get_procs()
{
//struct proc *process;
//process = malloc(nr_total * sizeof(struct proc);
struct proc process[nr_total];
parse_dir(process);
print_procs(process);
}
int mytop()
{
if (chdir("/proc") != 0)
{
perror("chdir to /proc" );
return 1;
}
print_memory();
getkinfo();
get_procs();
}

+ 655
- 352
mytop.c
File diff suppressed because it is too large
View File


Loading…
Cancel
Save