From 448d994bbaa00a0ffe1f98f766dc4e6f5282f3d3 Mon Sep 17 00:00:00 2001 From: Zhiyuan Shao Date: Mon, 8 May 2023 15:28:52 +0800 Subject: [PATCH] init commit of lab3_3 --- Makefile | 2 +- kernel/process.h | 3 +++ kernel/strap.c | 14 ++++++++++++++ user/{app_yield.c => app_two_long_loops.c} | 18 +++++++----------- 4 files changed, 25 insertions(+), 12 deletions(-) rename user/{app_yield.c => app_two_long_loops.c} (50%) diff --git a/Makefile b/Makefile index d11cc62..2dde5bf 100644 --- a/Makefile +++ b/Makefile @@ -70,7 +70,7 @@ USER_OBJS := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(USER_CPPS))) -USER_TARGET := $(OBJ_DIR)/app_yield +USER_TARGET := $(OBJ_DIR)/app_two_long_loops #------------------------targets------------------------ $(OBJ_DIR): @-mkdir -p $(OBJ_DIR) diff --git a/kernel/process.h b/kernel/process.h index 8f8bc9c..0a1bd9c 100644 --- a/kernel/process.h +++ b/kernel/process.h @@ -86,6 +86,9 @@ typedef struct process_t { struct process_t *parent; // next queue element struct process_t *queue_next; + + // accounting. added @lab3_3 + int tick_count; }process; // switch to run user app diff --git a/kernel/strap.c b/kernel/strap.c index 620c211..c6ac60c 100644 --- a/kernel/strap.c +++ b/kernel/strap.c @@ -67,6 +67,18 @@ void handle_user_page_fault(uint64 mcause, uint64 sepc, uint64 stval) { } } +// +// implements round-robin scheduling. added @lab3_3 +// +void rrsched() { + // TODO (lab3_3): implements round-robin scheduling. + // hint: increase the tick_count member of current process by one, if it is bigger than + // TIME_SLICE_LEN (means it has consumed its time slice), change its status into READY, + // place it in the rear of ready queue, and finally schedule next process to run. + panic( "You need to further implement the timer handling in lab3_3.\n" ); + +} + // // kernel/smode_trap.S will pass control to smode_trap_handler, when a trap happens // in S-mode. @@ -91,6 +103,8 @@ void smode_trap_handler(void) { break; case CAUSE_MTIMER_S_TRAP: handle_mtimer_trap(); + // invoke round-robin scheduler. added @lab3_3 + rrsched(); break; case CAUSE_STORE_PAGE_FAULT: case CAUSE_LOAD_PAGE_FAULT: diff --git a/user/app_yield.c b/user/app_two_long_loops.c similarity index 50% rename from user/app_yield.c rename to user/app_two_long_loops.c index bf2349c..2568485 100644 --- a/user/app_yield.c +++ b/user/app_two_long_loops.c @@ -1,6 +1,6 @@ /* - * The application of lab3_2. - * parent and child processes intermittently give up their processors. + * The application of lab3_3. + * parent and child processes never give up their processor during execution. */ #include "user/user_lib.h" @@ -8,22 +8,18 @@ int main(void) { uint64 pid = fork(); - uint64 rounds = 0xffff; + uint64 rounds = 100000000; + uint64 interval = 10000000; + uint64 a = 0; if (pid == 0) { printu("Child: Hello world! \n"); for (uint64 i = 0; i < rounds; ++i) { - if (i % 10000 == 0) { - printu("Child running %ld \n", i); - yield(); - } + if (i % interval == 0) printu("Child running %ld \n", i); } } else { printu("Parent: Hello world! \n"); for (uint64 i = 0; i < rounds; ++i) { - if (i % 10000 == 0) { - printu("Parent running %ld \n", i); - yield(); - } + if (i % interval == 0) printu("Parent running %ld \n", i); } }