Mercurial > hg > Members > shivanidubey > xv6
changeset 6:cbfb7472821d
priority_scheduler
author | shivanidubey |
---|---|
date | Wed, 19 Jun 2019 17:28:28 +0900 |
parents | 0428573d4327 |
children | 29025bcad36d |
files | src/proc.c src/proc.h src/syscall.c src/syscall.h src/sysproc.c src/usr/Makefile |
diffstat | 6 files changed, 53 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/src/proc.c Tue Jun 18 18:59:33 2019 +0900 +++ b/src/proc.c Wed Jun 19 17:28:28 2019 +0900 @@ -68,8 +68,9 @@ found: p->state = EMBRYO; p->pid = nextpid++; + p->priority = 10; release(&ptable.lock); - + // Allocate kernel stack. if((p->kstack = alloc_page ()) == 0){ p->state = UNUSED; @@ -309,6 +310,22 @@ sleep(proc, &ptable.lock); //DOC: wait-sleep } } + + +int get_highest_priority_proc(void) +{ + int highest_priority=ptable.proc->priority; + for(struct proc *p = ptable.proc; p < &ptable.proc[NPROC]; p++){ + if(p->state != RUNNABLE) { + continue; + } + if (highest_priority > p->priority) + { + highest_priority=p->priority; + } + } + return highest_priority; +} //PAGEBREAK: 42 // Per-CPU process scheduler. @@ -318,6 +335,7 @@ // - swtch to start running that process // - eventually that process transfers control // via swtch back to the scheduler. + void scheduler(void) { struct proc *p; @@ -328,30 +346,26 @@ // Loop over process table looking for process to run. acquire(&ptable.lock); - - for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ - if(p->state != RUNNABLE) { - continue; - } - + + int highest_priority = get_highest_priority_proc(); + // Switch to chosen process. It is the process's job // to release ptable.lock and then reacquire it // before jumping back to us. - proc = p; + struct proc *p =ptable.proc[highest_priority]; switchuvm(p); - + p->state = RUNNING; swtch(&cpu->scheduler, proc->context); + // Process is done running for now. // It should have changed its p->state before coming back. proc = 0; - } release(&ptable.lock); } } - // Enter scheduler. Must hold only ptable.lock // and have changed proc->state. void sched(void) @@ -530,3 +544,12 @@ } +void setpriority (int priority) +{ + proc->priority=priority; +} + +int getpriority () +{ + return proc->priority; +}
--- a/src/proc.h Tue Jun 18 18:59:33 2019 +0900 +++ b/src/proc.h Wed Jun 19 17:28:28 2019 +0900 @@ -67,6 +67,7 @@ struct file* ofile[NOFILE]; // Open files struct inode* cwd; // Current directory char name[16]; // Process name (debugging) + int priority; //Process priority }; // Process memory is laid out contiguously, low addresses first:
--- a/src/syscall.c Tue Jun 18 18:59:33 2019 +0900 +++ b/src/syscall.c Wed Jun 19 17:28:28 2019 +0900 @@ -114,6 +114,7 @@ extern int sys_wait(void); extern int sys_write(void); extern int sys_uptime(void); +extern void sys_setpriority(void); static int (*syscalls[])(void) = { [SYS_fork] sys_fork, @@ -137,6 +138,8 @@ [SYS_link] sys_link, [SYS_mkdir] sys_mkdir, [SYS_close] sys_close, + [SYS_getpriority] getpriority, + [SYS_setpriority] sys_setpriority, }; void syscall(void)
--- a/src/syscall.h Tue Jun 18 18:59:33 2019 +0900 +++ b/src/syscall.h Wed Jun 19 17:28:28 2019 +0900 @@ -20,3 +20,5 @@ #define SYS_link 19 #define SYS_mkdir 20 #define SYS_close 21 +#define SYS_setpriority 22 +#define SYS_getpriority 23