diff --git a/.gitignore b/.gitignore index 3e2c9deafe..854f80006d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,26 @@ kernel kernelmemfs mkfs .gdbinit +# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode +# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode \ No newline at end of file diff --git a/Makefile b/Makefile index 09d790cf63..8d02e3099a 100644 --- a/Makefile +++ b/Makefile @@ -181,6 +181,7 @@ UPROGS=\ _usertests\ _wc\ _zombie\ + _proc_dump\ fs.img: mkfs README $(UPROGS) ./mkfs fs.img README $(UPROGS) diff --git a/OS_P1.pdf b/OS_P1.pdf new file mode 100644 index 0000000000..a8d82c9c00 Binary files /dev/null and b/OS_P1.pdf differ diff --git a/commands.txt b/commands.txt new file mode 100644 index 0000000000..a90ac67b8f --- /dev/null +++ b/commands.txt @@ -0,0 +1,5 @@ +sudo apt install qemu-system-x86 +make qemu-nox +make clean +proc_dump +proc_dump 5 100 700000 1200000 2000000 3500000 \ No newline at end of file diff --git a/defs.h b/defs.h index 82fb982837..42605a4c02 100644 --- a/defs.h +++ b/defs.h @@ -120,6 +120,7 @@ void userinit(void); int wait(void); void wakeup(void*); void yield(void); +int sort_proc(void); // swtch.S void swtch(struct context**, struct context*); diff --git a/proc.c b/proc.c index 806b1b184b..6486f1c44d 100644 --- a/proc.c +++ b/proc.c @@ -12,6 +12,11 @@ struct { struct proc proc[NPROC]; } ptable; +// struct proc_info { +// int pid; +// int memsize; // in bytes +// }; + static struct proc *initproc; int nextpid = 1; @@ -532,3 +537,114 @@ procdump(void) cprintf("\n"); } } + +// struct proc_info* +int +sort_proc(void) +{ + int max; + int count = 0; + struct proc *p; + struct proc_info *result ; + + argint(0, &max); // max -> NPROC; // max number of processes = 6 + argptr(1, (char **)&result, max*sizeof(struct proc_info)); + + for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ + if(p->state == UNUSED) continue; + if(p->state == RUNNABLE || p->state == RUNNING){ + result[count].pid = p -> pid; + result[count].memsize = p -> sz; + count ++; + } + } + + // sort by memsize and pid + for(int i = 0; i < count; i++){ + for(int j = i + 1; j < count; j++){ + if(result[i].memsize > result[j].memsize){ + struct proc_info temp = result[i]; + result[i] = result[j]; + result[j] = temp; + + // temp.pid = result[i].pid; + // temp.memsize = result[i].memsize; + + // result[i].pid = result[j].pid; + // result[i].memsize = result[j].memsize; + + // result[j].pid = temp.pid; + // result[j].memsize = temp.memsize; + } + else if (result[i].memsize == result[j].memsize) + { + if(result[i].pid > result[j].pid){ + struct proc_info temp = result[i]; + result[i] = result[j]; + result[j] = temp; + } + } + } + } + + for(int i = 0; i < count; i++){ + cprintf("memsize:%d --process_id:%d\n", result[i].memsize, result[i].pid); // this works + } + + return count; + + + + // // static char *states[] = { + // // [UNUSED] "unused", + // // [EMBRYO] "embryo", + // // [SLEEPING] "sleep ", + // // [RUNNABLE] "runble", + // // [RUNNING] "run ", + // // [ZOMBIE] "zombie" + // // }; + // int i; + // struct proc *p; + // // char *state; + // // proc_info array + // // struct proc_info *result = (struct proc_info *)kalloc(); + // // struct proc_info result[NPROC] = (struct proc_info *)kalloc(sizeof(struct proc_info) * NPROC); + // // struct proc_info result[NPROC] = (struct proc_info *)kalloc(); + // struct proc_info result[NPROC]; + + // int count = 0; + // for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ + // // if(p->state == UNUSED) + // // continue; + // // if(p->state >= 0 && p->state < NELEM(states) && states[p->state]) + // if(p->state == RUNNABLE || p->state == RUNNING){ + // /// add to result + // result[count].pid = p->pid; + // result[count].memsize = p->sz; + // count++; + // } + // } + // // sort by memsize and pid + // for(i = 0; i < count; i++){ + // for(int j = i + 1; j < count; j++){ + // if(result[i].memsize > result[j].memsize){ + // struct proc_info temp = result[i]; + // result[i] = result[j]; + // result[j] = temp; + // } + // else if (result[i].memsize == result[j].memsize) + // { + // if(result[i].pid > result[j].pid){ + // struct proc_info temp = result[i]; + // result[i] = result[j]; + // result[j] = temp; + // } + // } + // } + // } + + // // print result + // for(i = 0; i < count; i++){ + // cprintf("memsize:%d --process_id:%d\n", result[i].memsize, result[i].pid); + // } +} \ No newline at end of file diff --git a/proc_dump.c b/proc_dump.c new file mode 100644 index 0000000000..c60561f2d3 --- /dev/null +++ b/proc_dump.c @@ -0,0 +1,173 @@ +#include "types.h" +#include "stat.h" +#include "user.h" +#include "param.h" + +// reverse: reverse string s in place +void +reverse(char s[]) +{ + int c, i, j; + + for (i = 0, j = strlen(s)-1; i 0); // delete it + if(sign < 0) + s[i++] = '-'; + s[i] = '\0'; + reverse(s); +} + + +int main(int argc, char *argv[]) +{ + /// proc_dump 5 10000 25000 25000 50000 68000 (5 processes add to current) + // proc_dump 5 100 700000 1200000 2000000 3500000 + for (int i = 0; i < argc; i++) + { + int pid = fork(); + if (pid == 0) + { + malloc(atoi(argv[i])); + + while (1) + { + } + // long tmp = 0; + // for (unsigned int j = 0; j < 0xFFFFFFFF; j++) + // { + // for (unsigned int k = 0; k < 0xFFFFFFFF; k++) + // { + // tmp += j * k; + // } + // } + exit(); + } + } + int maxProcess = NPROC; // max number of processes = 64 + struct proc_info *processes = malloc(maxProcess * sizeof(struct proc_info)); + + int Number = proc_dump(maxProcess, processes); + for (int i = 0; i < Number; i++) + { + // printf(1, "memsize:%d -- process_id:%d\n", processes[i].memsize, processes[i].pid); + char str1[50]; + char str2[50]; + itoa(str1, processes[i].memsize); + itoa(str2, processes[i].pid); + printf(1, "memsize:"); + printf(1, str1); + printf(1, " -- process_id:"); + printf(1, str2); + printf(1, "\n"); + } + // for (int i = 0; i < argc; i++) + // { + // wait(); + // } + + // for (int i = 0; i < argc; i++) + // { + // kill(pids[i]); + // } + exit(); + + // ----------------------------------------------- + + // int r = fork(); + // if (r == 0) + // { + // r = fork(); + // if (r != 0) + // { + // // int: 4 bytes + // // 32 bytes of memory block + // // malloc(8 * sizeof(int)); + // malloc(100000); + // for (unsigned int i = 0; i < 0xFFFFFFFF; i++) + // { + // } + // for (unsigned int j = 0; j < 0xFFFFFFFF; j++) + // { + // for (unsigned int k = 0; k < 0xFFFFFFFF; k++) + // { + // } + // } + // } + // else + // { + // // 40 bytes of memory block + // // malloc(10 * sizeof(int)); + // // malloc(10000); + // malloc(100000); + // for (unsigned int i = 0; i < 0xFFFFFFFF; i++) + // { + // } + // for (unsigned int j = 0; j < 0xFFFFFFFF; j++) + // { + // for (unsigned int k = 0; k < 0xFFFFFFFF; k++) + // { + // } + // } + // } + // wait(); + // } + // else + // { + // r = fork(); + // if (r == 0) + // { + // // 60 bytes of memory block + // // malloc(15 * sizeof(int)); + // malloc(30000); + // for (unsigned int i = 0; i < 0xFFFFFFFF; i++) + // { + // } + // for (unsigned int j = 0; j < 0xFFFFFFFF; j++) + // { + // for (unsigned int k = 0; k < 0xFFFFFFFF; k++) + // { + // } + // } + // // wait(); + // } + // else + // { + // // 20 bytes of memory block + // // malloc(5 * sizeof(int)); + // malloc(40000); + // int maxProcess = NPROC; // max number of processes = 64 + // struct proc_info *processes = malloc(maxProcess * sizeof(struct proc_info)); + + // int Number = proc_dump(maxProcess, processes); + // for (int i = 0; i < Number; i++) + // { + // // printf(2, processes[i].pid, processes[i].memsize); + // printf(1, "memsize:%d -- process_id:%d\n", processes[i].memsize, processes[i].pid); + // } + // wait(); + // } + // } + + // wait(); + // wait(); + // exit(); + // return 0; + + } diff --git a/syscall.c b/syscall.c index ee85261602..585519283f 100644 --- a/syscall.c +++ b/syscall.c @@ -103,6 +103,7 @@ extern int sys_unlink(void); extern int sys_wait(void); extern int sys_write(void); extern int sys_uptime(void); +extern int sys_proc_dump(void); static int (*syscalls[])(void) = { [SYS_fork] sys_fork, @@ -126,6 +127,7 @@ static int (*syscalls[])(void) = { [SYS_link] sys_link, [SYS_mkdir] sys_mkdir, [SYS_close] sys_close, +[SYS_proc_dump] sys_proc_dump, }; void diff --git a/syscall.h b/syscall.h index bc5f35651c..eec5851eac 100644 --- a/syscall.h +++ b/syscall.h @@ -20,3 +20,4 @@ #define SYS_link 19 #define SYS_mkdir 20 #define SYS_close 21 +#define SYS_proc_dump 22 \ No newline at end of file diff --git a/sysproc.c b/sysproc.c index 0686d295b6..786f92f2bb 100644 --- a/sysproc.c +++ b/sysproc.c @@ -89,3 +89,13 @@ sys_uptime(void) release(&tickslock); return xticks; } + +int +sys_proc_dump(void) +{ + sort_proc(); + // cprintf("Hello world\n"); + // return 12; + // proc_dump(); + return 0; +} \ No newline at end of file diff --git a/types.h b/types.h index e4adf644ae..66c6dc49d4 100644 --- a/types.h +++ b/types.h @@ -2,3 +2,10 @@ typedef unsigned int uint; typedef unsigned short ushort; typedef unsigned char uchar; typedef uint pde_t; + +// Project Struct as said in the project description +struct proc_info +{ + int pid; + int memsize; +}; \ No newline at end of file diff --git a/user.h b/user.h index 4f99c52ba6..7ef423aba2 100644 --- a/user.h +++ b/user.h @@ -23,6 +23,7 @@ int getpid(void); char* sbrk(int); int sleep(int); int uptime(void); +int proc_dump(int , void*); // ulib.c int stat(const char*, struct stat*); diff --git a/usys.S b/usys.S index 8bfd8a1bc4..4be6770403 100644 --- a/usys.S +++ b/usys.S @@ -29,3 +29,4 @@ SYSCALL(getpid) SYSCALL(sbrk) SYSCALL(sleep) SYSCALL(uptime) +SYSCALL(proc_dump) \ No newline at end of file diff --git "a/\332\257\330\262\330\247\330\261\330\264.pdf" "b/\332\257\330\262\330\247\330\261\330\264.pdf" new file mode 100644 index 0000000000..9ba566d854 Binary files /dev/null and "b/\332\257\330\262\330\247\330\261\330\264.pdf" differ