|
@ -2,9 +2,10 @@ |
|
|
#include <list.h> |
|
|
#include <list.h> |
|
|
#include <proc.h> |
|
|
#include <proc.h> |
|
|
#include <assert.h> |
|
|
#include <assert.h> |
|
|
|
|
|
// #include <time.h> // 没有这个文件 |
|
|
#include <default_sched.h> |
|
|
#include <default_sched.h> |
|
|
|
|
|
|
|
|
#define USE_SKEW_HEAP 1 |
|
|
|
|
|
|
|
|
#define USE_SKEW_HEAP 0 |
|
|
|
|
|
|
|
|
/* You should define the BigStride constant here*/ |
|
|
/* You should define the BigStride constant here*/ |
|
|
/* LAB6: YOUR CODE */ |
|
|
/* LAB6: YOUR CODE */ |
|
@ -152,6 +153,85 @@ stride_proc_tick(struct run_queue *rq, struct proc_struct *proc) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void |
|
|
|
|
|
ticket_init(struct run_queue *rq) { |
|
|
|
|
|
list_init(&(rq->run_list)); |
|
|
|
|
|
rq->lab6_run_pool = NULL; |
|
|
|
|
|
rq->proc_num = 0; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void |
|
|
|
|
|
ticket_enqueue(struct run_queue *rq, struct proc_struct *proc) { |
|
|
|
|
|
/* LAB6: YOUR CODE */ |
|
|
|
|
|
#if USE_SKEW_HEAP |
|
|
|
|
|
rq->lab6_run_pool = |
|
|
|
|
|
skew_heap_insert(rq->lab6_run_pool, &(proc->lab6_run_pool), proc_stride_comp_f); |
|
|
|
|
|
#else |
|
|
|
|
|
assert(list_empty(&(proc->run_link))); |
|
|
|
|
|
list_add_before(&(rq->run_list), &(proc->run_link)); |
|
|
|
|
|
#endif |
|
|
|
|
|
if (proc->time_slice == 0 || proc->time_slice > rq->max_time_slice) { |
|
|
|
|
|
proc->time_slice = rq->max_time_slice; |
|
|
|
|
|
} |
|
|
|
|
|
proc->rq = rq; |
|
|
|
|
|
proc->lab6_ticket_start = rq->lab6_total_num + 1; |
|
|
|
|
|
rq->proc_num ++; |
|
|
|
|
|
rq->lab6_total_num += proc->lab6_priority + 1; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static struct proc_struct * |
|
|
|
|
|
ticket_pick_next(struct run_queue *rq) { |
|
|
|
|
|
/* LAB6: YOUR CODE */ |
|
|
|
|
|
uint32_t target_index = rand() % rq->lab6_total_num; |
|
|
|
|
|
#if USE_SKEW_HEAP |
|
|
|
|
|
if (rq->lab6_run_pool == NULL) return NULL; |
|
|
|
|
|
struct proc_struct *p = le2proc(rq->lab6_run_pool, lab6_run_pool); |
|
|
|
|
|
#else |
|
|
|
|
|
list_entry_t *le = list_next(&(rq->run_list)); |
|
|
|
|
|
|
|
|
|
|
|
if (le == &rq->run_list) |
|
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
|
|
|
|
struct proc_struct *p; |
|
|
|
|
|
int32_t temp_ticket_sum = 0; |
|
|
|
|
|
while (le != &rq->run_list) |
|
|
|
|
|
{ |
|
|
|
|
|
p = le2proc(le, run_link); |
|
|
|
|
|
temp_ticket_sum += p->lab6_priority + 1; |
|
|
|
|
|
if (temp_ticket_sum >= target_index) |
|
|
|
|
|
break; |
|
|
|
|
|
le = list_next(le); |
|
|
|
|
|
} |
|
|
|
|
|
#endif |
|
|
|
|
|
return p; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void |
|
|
|
|
|
ticket_dequeue(struct run_queue *rq, struct proc_struct *proc) { |
|
|
|
|
|
/* LAB6: YOUR CODE */ |
|
|
|
|
|
#if USE_SKEW_HEAP |
|
|
|
|
|
rq->lab6_run_pool = |
|
|
|
|
|
skew_heap_remove(rq->lab6_run_pool, &(proc->lab6_run_pool), proc_stride_comp_f); |
|
|
|
|
|
#else |
|
|
|
|
|
assert(!list_empty(&(proc->run_link)) && proc->rq == rq); |
|
|
|
|
|
list_del_init(&(proc->run_link)); |
|
|
|
|
|
#endif |
|
|
|
|
|
rq->proc_num --; |
|
|
|
|
|
rq->lab6_total_num -= proc->lab6_priority; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void |
|
|
|
|
|
ticket_proc_tick(struct run_queue *rq, struct proc_struct *proc) { |
|
|
|
|
|
/* LAB6: YOUR CODE */ |
|
|
|
|
|
if (proc->time_slice > 0) { |
|
|
|
|
|
proc->time_slice --; |
|
|
|
|
|
} |
|
|
|
|
|
if (proc->time_slice == 0) { |
|
|
|
|
|
proc->need_resched = 1; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
struct sched_class default_sched_class = { |
|
|
struct sched_class default_sched_class = { |
|
|
.name = "stride_scheduler", |
|
|
.name = "stride_scheduler", |
|
|
.init = stride_init, |
|
|
.init = stride_init, |
|
@ -161,3 +241,11 @@ struct sched_class default_sched_class = { |
|
|
.proc_tick = stride_proc_tick, |
|
|
.proc_tick = stride_proc_tick, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct sched_class ticket_sched_class = { |
|
|
|
|
|
.name = "ticket_scheduler", |
|
|
|
|
|
.init = ticket_init, |
|
|
|
|
|
.enqueue = ticket_enqueue, |
|
|
|
|
|
.dequeue = ticket_dequeue, |
|
|
|
|
|
.pick_next = ticket_pick_next, |
|
|
|
|
|
.proc_tick = ticket_proc_tick, |
|
|
|
|
|
}; |