From f05490569c74d147d4995a64b665ac75d5cb6d8e Mon Sep 17 00:00:00 2001 From: Yunhao Date: Wed, 30 Mar 2022 17:25:25 -0400 Subject: [PATCH] cleanup --- apps/system/sys_dir.c | 39 ++++++++++++++++++--------------------- apps/system/sys_file.c | 4 +--- apps/system/sys_proc.c | 22 +++++++--------------- grass/grass.c | 12 ++++++------ grass/grass.h | 7 ------- library/egos.h | 4 ++++ 6 files changed, 36 insertions(+), 52 deletions(-) diff --git a/apps/system/sys_dir.c b/apps/system/sys_dir.c index 9afcb76e..f29bb943 100644 --- a/apps/system/sys_dir.c +++ b/apps/system/sys_dir.c @@ -10,7 +10,24 @@ #include "app.h" #include -int dir_do_lookup(int ino, char* name); +int dir_do_lookup(int dir_ino, char* name) { + char block[BLOCK_SIZE]; + file_read(dir_ino, 0, block); + + int dir_len = strlen(block); + int name_len = strlen(name); + + for (int i = 0, ret = 0; i < dir_len - name_len; i++) + if (strncmp(name, block + i, name_len) == 0 && + block[i + name_len] == ' ') { + for (int k = 0; k < 4; k++) + if (block[i + name_len + k] != ' ') + ret = ret * 10 + block[i + name_len + k] - '0'; + return ret; + } + + return -1; +} int main() { SUCCESS("Enter kernel process GPID_DIR"); @@ -40,23 +57,3 @@ int main() { } } } - -int dir_do_lookup(int dir_ino, char* name) { - char block[BLOCK_SIZE]; - file_read(dir_ino, 0, block); - - int dir_len = strlen(block); - int name_len = strlen(name); - - for (int i = 0; i < dir_len - name_len; i++) - if (strncmp(name, block + i, name_len) == 0 && - block[i + name_len] == ' ') { - int ino = 0, base = i + name_len; - for (int k = 0; k < 4; k++) - if (block[base + k] != ' ') - ino = ino * 10 + block[base + k] - '0'; - return ino; - } - - return -1; -} diff --git a/apps/system/sys_file.c b/apps/system/sys_file.c index 52d3ba05..5edf690c 100644 --- a/apps/system/sys_file.c +++ b/apps/system/sys_file.c @@ -11,8 +11,6 @@ #include "file.h" #include -block_if fs; - int main() { SUCCESS("Enter kernel process GPID_FILE"); @@ -20,7 +18,7 @@ int main() { block_if disk = fs_disk_init(); if (treedisk_create(disk, 0, NINODES) < 0) FATAL("proc_file: can't create treedisk file system"); - fs = treedisk_init(disk, 0); + block_if fs = treedisk_init(disk, 0); /* Send notification to GPID_PROCESS */ char buf[SYSCALL_MSG_LEN]; diff --git a/apps/system/sys_proc.c b/apps/system/sys_proc.c index 6a7c596a..42d28a07 100644 --- a/apps/system/sys_proc.c +++ b/apps/system/sys_proc.c @@ -12,20 +12,12 @@ #include "disk.h" #include -/* same as the struct pcb_intf in grass/process.h */ -struct pcb_intf { - int (*proc_alloc)(); - void (*proc_free)(int); - void (*proc_set_ready)(int); -} pcb; - static int app_ino, app_pid; static void sys_spawn(int base); static int app_spawn(struct proc_request *req); -int main(struct pcb_intf* _pcb) { +int main() { SUCCESS("Enter kernel process GPID_PROCESS"); - memcpy(&pcb, _pcb, sizeof(struct pcb_intf)); int sender, shell_waiting; char buf[SYSCALL_MSG_LEN]; @@ -53,14 +45,14 @@ int main(struct pcb_intf* _pcb) { INFO("process %d running in the background", app_pid); sys_send(GPID_SHELL, (void*)reply, sizeof(reply)); } else if (req->type == PROC_EXIT) { - pcb.proc_free(sender); + grass->proc_free(sender); if (shell_waiting && app_pid == sender) sys_send(GPID_SHELL, (void*)reply, sizeof(reply)); else INFO("background process %d terminated", sender); } else if (req->type == PROC_KILLALL){ - pcb.proc_free(-1); + grass->proc_free(-1); } } } @@ -73,11 +65,11 @@ static int app_spawn(struct proc_request *req) { int bin_ino = dir_lookup(0, "bin"); if ((app_ino = dir_lookup(bin_ino, req->argv[0])) < 0) return -1; - app_pid = pcb.proc_alloc(); + app_pid = grass->proc_alloc(); int argc = req->argv[req->argc - 1][0] == '&'? req->argc - 1 : req->argc; elf_load(app_pid, app_read, argc, (void**)req->argv); - pcb.proc_set_ready(app_pid); + grass->proc_set_ready(app_pid); return 0; } @@ -88,10 +80,10 @@ static int sys_proc_read(int block_no, char* dst) { char* sysproc_names[] = {"sys_proc", "sys_file", "sys_dir", "sys_shell"}; static void sys_spawn(int base) { - int pid = pcb.proc_alloc(); + int pid = grass->proc_alloc(); INFO("Load kernel process #%d: %s", pid, sysproc_names[pid - 1]); sys_proc_base = base; elf_load(pid, sys_proc_read, 0, NULL); - pcb.proc_set_ready(pid); + grass->proc_set_ready(pid); } diff --git a/grass/grass.c b/grass/grass.c index 67d56ef1..50b0356f 100644 --- a/grass/grass.c +++ b/grass/grass.c @@ -11,8 +11,8 @@ #include "egos.h" #include "grass.h" -struct pcb_intf pcb; struct earth *earth = (void*)EARTH_STRUCT; +struct grass *grass = (void*)GRASS_STRUCT; void proc_init(); void timer_init(); @@ -26,11 +26,11 @@ int main() { sys_proc_spawn(); /* Enter the first kernel process sys_proc */ - void (*sys_proc_entry)(void*) = (void*)APPS_ENTRY; + void (*sys_proc_entry)() = (void*)APPS_ENTRY; earth->mmu_switch(GPID_PROCESS); /* setup virtual address space */ timer_reset(); /* start timer */ earth->intr_enable(); /* enable interrupt */ - sys_proc_entry(&pcb); /* enter sys_proc */ + sys_proc_entry(); /* enter sys_proc */ FATAL("Should never return to the grass kernel main()"); } @@ -43,7 +43,7 @@ static void sys_proc_spawn() { INFO("Load kernel process #%d: sys_proc", GPID_PROCESS); elf_load(GPID_PROCESS, read_sys_proc, 0, NULL); - pcb.proc_alloc = proc_alloc; - pcb.proc_free = proc_free; - pcb.proc_set_ready = proc_set_ready; + grass->proc_alloc = proc_alloc; + grass->proc_free = proc_free; + grass->proc_set_ready = proc_set_ready; } diff --git a/grass/grass.h b/grass/grass.h index 1c91f899..4c9006c8 100644 --- a/grass/grass.h +++ b/grass/grass.h @@ -28,13 +28,6 @@ enum { PROC_WAIT_TO_RECV }; -/* interface of the process control block (pcb) */ -struct pcb_intf { - int (*proc_alloc)(); - void (*proc_free)(int); - void (*proc_set_ready)(int); -}; - long long timer_reset(); int proc_alloc(); diff --git a/library/egos.h b/library/egos.h index 0161701d..ee4588df 100644 --- a/library/egos.h +++ b/library/egos.h @@ -27,6 +27,10 @@ struct earth { }; struct grass { + int (*proc_alloc)(); + void (*proc_free)(int); + void (*proc_set_ready)(int); + int work_dir_ino; char work_dir[32 * 16]; char work_dir_name[32];