diff --git a/Makefile b/Makefile index 8c1e1f7..d11cc62 100644 --- a/Makefile +++ b/Makefile @@ -70,7 +70,7 @@ USER_OBJS := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(USER_CPPS))) -USER_TARGET := $(OBJ_DIR)/app_naive_fork +USER_TARGET := $(OBJ_DIR)/app_yield #------------------------targets------------------------ $(OBJ_DIR): @-mkdir -p $(OBJ_DIR) diff --git a/kernel/syscall.c b/kernel/syscall.c index b5ee586..d2b1fd1 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -68,6 +68,19 @@ ssize_t sys_user_fork() { return do_fork( current ); } +// +// kerenl entry point of yield +// +ssize_t sys_user_yield() { + // TODO (lab3_2): implment the syscall of yield. + // hint: the functionality of yield is to give up the processor. therefore, + // we should set the status of currently running process to READY, insert it in + // the rear of ready queue, and finally, schedule a READY process to run. + panic( "You need to implement the yield syscall in lab3_2.\n" ); + + return 0; +} + // // [a0]: the syscall number; [a1] ... [a7]: arguments to the syscalls. // returns the code of success, (e.g., 0 means success, fail for otherwise) @@ -84,6 +97,8 @@ long do_syscall(long a0, long a1, long a2, long a3, long a4, long a5, long a6, l return sys_user_free_page(a1); case SYS_user_fork: return sys_user_fork(); + case SYS_user_yield: + return sys_user_yield(); default: panic("Unknown syscall %ld \n", a0); } diff --git a/kernel/syscall.h b/kernel/syscall.h index 13332b7..e4e02fe 100644 --- a/kernel/syscall.h +++ b/kernel/syscall.h @@ -11,6 +11,7 @@ #define SYS_user_allocate_page (SYS_user_base + 2) #define SYS_user_free_page (SYS_user_base + 3) #define SYS_user_fork (SYS_user_base + 4) +#define SYS_user_yield (SYS_user_base + 5) long do_syscall(long a0, long a1, long a2, long a3, long a4, long a5, long a6, long a7); diff --git a/user/app_naive_fork.c b/user/app_naive_fork.c deleted file mode 100644 index 06ed1b9..0000000 --- a/user/app_naive_fork.c +++ /dev/null @@ -1,18 +0,0 @@ -/* - * The application of lab3_1. - * it simply forks a child process. - */ - -#include "user/user_lib.h" -#include "util/types.h" - -int main(void) { - uint64 pid = fork(); - if (pid == 0) { - printu("Child: Hello world!\n"); - } else { - printu("Parent: Hello world! child id %ld\n", pid); - } - - exit(0); -} diff --git a/user/app_yield.c b/user/app_yield.c new file mode 100644 index 0000000..bf2349c --- /dev/null +++ b/user/app_yield.c @@ -0,0 +1,32 @@ +/* + * The application of lab3_2. + * parent and child processes intermittently give up their processors. + */ + +#include "user/user_lib.h" +#include "util/types.h" + +int main(void) { + uint64 pid = fork(); + uint64 rounds = 0xffff; + 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(); + } + } + } else { + printu("Parent: Hello world! \n"); + for (uint64 i = 0; i < rounds; ++i) { + if (i % 10000 == 0) { + printu("Parent running %ld \n", i); + yield(); + } + } + } + + exit(0); + return 0; +} diff --git a/user/user_lib.c b/user/user_lib.c index f3756d8..3fcb85d 100644 --- a/user/user_lib.c +++ b/user/user_lib.c @@ -69,3 +69,10 @@ void naive_free(void* va) { int fork() { return do_user_call(SYS_user_fork, 0, 0, 0, 0, 0, 0, 0); } + +// +// lib call to yield +// +void yield() { + do_user_call(SYS_user_yield, 0, 0, 0, 0, 0, 0, 0); +} diff --git a/user/user_lib.h b/user/user_lib.h index e1c4207..63e2e25 100644 --- a/user/user_lib.h +++ b/user/user_lib.h @@ -7,3 +7,4 @@ int exit(int code); void* naive_malloc(); void naive_free(void* va); int fork(); +void yield();