Commit 3ece5945 authored by Benoît du Garreau's avatar Benoît du Garreau
Browse files

Add diffs

parent 11deef16
diff --git a/kernel/proc.c b/kernel/proc.c
index deccc39..e817891 100644
--- a/kernel/proc.c
+++ b/kernel/proc.c
@@ -503,11 +503,12 @@ wait(uint64 addr)
}
}
-struct proc* pick_highest_priority_runnable_proc() {
+static struct proc* pick_highest_priority_runnable_proc() {
acquire(&prio_lock);
struct list_proc** lp;
for (lp = prio; lp < &prio[NPRIO]; lp++) {
struct list_proc* lp_item = *lp;
+ if(lp_item) {
do {
struct proc* p = lp_item -> p;
acquire(&p->lock);
@@ -515,11 +516,22 @@ struct proc* pick_highest_priority_runnable_proc() {
return p;
}
release(&p->lock);
- } while (lp_item -> next);
+ }while ((lp_item = lp_item->next));
+ }
}
release(&prio_lock);
return 0;
}
+
+void print_list(int priority) {
+ struct list_proc* lastlp = prio[priority];
+ while (lastlp) {
+ printf("%p -> ", lastlp);
+ lastlp = lastlp->next;
+ }
+ printf("(nil)\n");
+}
+
// Per-CPU process scheduler.
// Each CPU calls scheduler() after setting itself up.
// Scheduler never returns. It loops, doing:
@@ -543,31 +555,44 @@ scheduler(void)
// cause a lost wakeup.
intr_off();
- int found = 0;
- for(p = proc; p < &proc[NPROC]; p++) {
- acquire(&p->lock);
- if(p->state == RUNNABLE) {
- // Switch to chosen process. It is the process's job
- // to release its lock and then reacquire it
- // before jumping back to us.
- p->state = RUNNING;
- c->proc = p;
- swtch(&c->scheduler, &p->context);
-
- // Process is done running for now.
- // It should have changed its p->state before coming back.
- c->proc = 0;
-
- found = 1;
+ if ((p = pick_highest_priority_runnable_proc())) {
+ // Move to the end of priority queue
+ struct list_proc* before_lp = 0;
+ struct list_proc* lastlp = prio[p->priority];
+ while (lastlp->next)
+ {
+ if(lastlp->next->p == p)
+ before_lp = lastlp;
+ lastlp = lastlp->next;
+ };
+ if(before_lp) {
+ lastlp->next = before_lp->next;
+ before_lp->next = before_lp->next->next;
+ lastlp->next->next = 0;
+ } else {
+ struct list_proc* chosen = prio[p->priority];
+ lastlp->next = chosen;
+ prio[p->priority] = chosen->next;
+ chosen->next = 0;
}
+ release(&prio_lock);
+ // Switch to chosen process. It is the process's job
+ // to release its lock and then reacquire it
+ // before jumping back to us.
+ p->state = RUNNING;
+ c->proc = p;
+ swtch(&c->scheduler, &p->context);
+
+ // Process is done running for now.
+ // It should have changed its p->state before coming back.
+ c->proc = 0;
// ensure that release() doesn't enable interrupts.
// again to avoid a race between interrupt and WFI.
c->intena = 0;
release(&p->lock);
- }
- if(found == 0){
+ }else {
asm volatile("wfi");
}
}
diff --git a/kernel/defs.h b/kernel/defs.h
index df618ee..07ea9ba 100644
--- a/kernel/defs.h
+++ b/kernel/defs.h
@@ -92,6 +92,7 @@ void printfinit(void);
// proc.c
int cpuid(void);
void exit(int);
+int nice(int, int);
int fork(void);
int nice(int,int);
int growproc(int);
diff --git a/kernel/proc.c b/kernel/proc.c
index e817891..a2353d4 100644
--- a/kernel/proc.c
+++ b/kernel/proc.c
@@ -299,6 +299,10 @@ growproc(int n)
return 0;
}
+int nice(int pid, int priority) {
+ return -1;
+}
+
// Create a new process, copying the parent.
// Sets up child kernel stack to return as if from fork() system call.
int
diff --git a/kernel/sysproc.c b/kernel/sysproc.c
index e4e45c8..510f34d 100644
--- a/kernel/sysproc.c
+++ b/kernel/sysproc.c
@@ -75,7 +75,16 @@ sys_sleep(void)
uint64
sys_nice(void){
- return 0;
+ int pid;
+ int priority;
+
+ if(argint(0, &pid) < 0)
+ return -1;
+ if(argint(1, &priority) < 0)
+ return -1;
+ if(priority < 0 || priority >= NPRIO)
+ return -1;
+ return nice(pid, priority);
}
uint64
diff --git a/kernel/proc.c b/kernel/proc.c
index a2353d4..7f536a0 100644
--- a/kernel/proc.c
+++ b/kernel/proc.c
@@ -300,7 +300,28 @@ growproc(int n)
}
int nice(int pid, int priority) {
+ struct proc *p;
+
+ acquire(&prio_lock);
+
+ for(p = proc; p < &proc[NPROC]; p++){
+ acquire(&p->lock);
+ if(p->pid == pid){
+ goto found;
+ }
+ release(&p->lock);
+ }
+ release(&prio_lock);
return -1;
+
+found:
+ remove_from_prio_queue(p);
+ p->priority = priority;
+ insert_into_prio_queue(p);
+
+ release(&p->lock);
+ release(&prio_lock);
+ return 0;
}
// Create a new process, copying the parent.
diff --git a/kernel/file.h b/kernel/file.h
index aa6d208..f1d2d3b 100644
--- a/kernel/file.h
+++ b/kernel/file.h
@@ -2,7 +2,7 @@
#define FILE_H
struct file {
- enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE } type;
+ enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE, FD_MUTEX } type;
int ref; // reference count
char readable;
char writable;
@@ -11,6 +11,7 @@ struct file {
uint off; // FD_INODE and FD_DEVICE
short major; // FD_DEVICE
short minor; // FD_DEVICE
+ struct sleeplock mutex; // FD_MUTEX
};
#define major(dev) ((dev) >> 16 & 0xFFFF)
diff --git a/kernel/sysfile.c b/kernel/sysfile.c
index a1975ed..442be9f 100644
--- a/kernel/sysfile.c
+++ b/kernel/sysfile.c
@@ -486,7 +486,18 @@ sys_pipe(void)
uint64
sys_create_mutex(void)
{
- return -1;
+ int fd;
+ struct file *f;
+ if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
+ if(f)
+ fileclose(f);
+ return -1;
+ }
+ f->type = FD_MUTEX;
+ f->readable = 0;
+ f->writable = 0;
+ initsleeplock(&f->mutex,"mutex");
+ return fd;
}
uint64
diff --git a/kernel/sysfile.c b/kernel/sysfile.c
index 442be9f..86b7f10 100644
--- a/kernel/sysfile.c
+++ b/kernel/sysfile.c
@@ -503,12 +503,33 @@ sys_create_mutex(void)
uint64
sys_acquire_mutex(void)
{
+ struct file *f;
+ if(argfd(0, 0, &f) < 0)
+ return -1;
+ if(f->type != FD_MUTEX)
+ return -1;
+ acquiresleep(&f->mutex);
+ struct proc *p = myproc();
+ if(p == 0)
+ panic("sys_acquire_mutex");
+ f->mutex.pid = p->pid;
return 0;
}
uint64
sys_release_mutex(void)
{
+ struct file *f;
+ if(argfd(0, 0, &f) < 0)
+ return -1;
+ if(f->type != FD_MUTEX)
+ return -1;
+ struct proc *p = myproc();
+ if(p == 0)
+ return -1;
+ if (p->pid != f->mutex.pid)
+ return -1;
+ releasesleep(&f->mutex);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment