Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fb phase1 #186

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ UPROGS=\
_usertests\
_wc\
_zombie\
_proc_dump\

fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
Expand Down
Binary file added OS_P1.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions commands.txt
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
Expand Down
116 changes: 116 additions & 0 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
// }
}
173 changes: 173 additions & 0 deletions proc_dump.c
Original file line number Diff line number Diff line change
@@ -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<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}

// itoa: convert n to characters in s
void
itoa(char s[], int n)
{
int i, sign;

if((sign = n) < 0) // record sign
n = -n; // make n positive
i = 0;
do { // generate digits in reverse order
s[i++] = n % 10 + '0'; // get next digit
} while ((n /= 10) > 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;

}
2 changes: 2 additions & 0 deletions syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
#define SYS_link 19
#define SYS_mkdir 20
#define SYS_close 21
#define SYS_proc_dump 22
10 changes: 10 additions & 0 deletions sysproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading