Browse Source

实验6 第一版

彩票调度算法
x86-32
423A35C7 3 months ago
parent
commit
5e2775f9da
6 changed files with 110 additions and 2 deletions
  1. +17
    -0
      labcodes_answer/lab6_result/kern/process/proc.c
  2. +1
    -0
      labcodes_answer/lab6_result/kern/process/proc.h
  3. +89
    -1
      labcodes_answer/lab6_result/kern/schedule/default_sched.c
  4. +1
    -0
      labcodes_answer/lab6_result/kern/schedule/default_sched.h
  5. +1
    -1
      labcodes_answer/lab6_result/kern/schedule/sched.c
  6. +1
    -0
      labcodes_answer/lab6_result/kern/schedule/sched.h

+ 17
- 0
labcodes_answer/lab6_result/kern/process/proc.c View File

@ -820,9 +820,19 @@ user_main(void *arg) {
panic("user_main execve failed.\n");
}
static int
my_test_user_main(void *arg) {
int num = (int)arg;
int priority = (rand() % 100) * 100;
cprintf("process %d, priority %d\n", num, priority);
lab6_set_priority(priority);
cprintf("process %d end\n", num);
}
// init_main - the second kernel thread used to create user_main kernel threads
static int
init_main(void *arg) {
srand((unsigned int)0);
size_t nr_free_pages_store = nr_free_pages();
size_t kernel_allocated_store = kallocated();
@ -831,6 +841,13 @@ init_main(void *arg) {
panic("create user_main failed.\n");
}
// error: for loop initial declarations are only allowed in C99 mode
int i = 0;
for (; i < 10; i++) {
int pid = kernel_thread(my_test_user_main, (void *)i, 0);
struct proc_strucht *proc = find_proc(pid);
}
while (do_wait(0, NULL) == 0) {
schedule();
}

+ 1
- 0
labcodes_answer/lab6_result/kern/process/proc.h View File

@ -64,6 +64,7 @@ struct proc_struct {
skew_heap_entry_t lab6_run_pool; // FOR LAB6 ONLY: the entry in the run pool
uint32_t lab6_stride; // FOR LAB6 ONLY: the current stride of the process
uint32_t lab6_priority; // FOR LAB6 ONLY: the priority of process, set by lab6_set_priority(uint32_t)
uint32_t lab6_ticket_start;
};
#define PF_EXITING 0x00000001 // getting shutdown

+ 89
- 1
labcodes_answer/lab6_result/kern/schedule/default_sched.c View File

@ -2,9 +2,10 @@
#include <list.h>
#include <proc.h>
#include <assert.h>
// #include <time.h> //
#include <default_sched.h>
#define USE_SKEW_HEAP 1
#define USE_SKEW_HEAP 0
/* You should define the BigStride constant here*/
/* 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 = {
.name = "stride_scheduler",
.init = stride_init,
@ -161,3 +241,11 @@ struct sched_class default_sched_class = {
.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,
};

+ 1
- 0
labcodes_answer/lab6_result/kern/schedule/default_sched.h View File

@ -4,6 +4,7 @@
#include <sched.h>
extern struct sched_class default_sched_class;
extern struct sched_class ticket_sched_class;
#endif /* !__KERN_SCHEDULE_SCHED_RR_H__ */

+ 1
- 1
labcodes_answer/lab6_result/kern/schedule/sched.c View File

@ -46,7 +46,7 @@ void
sched_init(void) {
list_init(&timer_list);
sched_class = &default_sched_class;
sched_class = &ticket_sched_class;
rq = &__rq;
rq->max_time_slice = MAX_TIME_SLICE;

+ 1
- 0
labcodes_answer/lab6_result/kern/schedule/sched.h View File

@ -42,6 +42,7 @@ struct run_queue {
int max_time_slice;
// For LAB6 ONLY
skew_heap_entry_t *lab6_run_pool;
uint32_t lab6_total_num;
};
void sched_init(void);

Loading…
Cancel
Save