Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yhzhang0128 committed Mar 30, 2022
1 parent dfc82b9 commit f054905
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 52 deletions.
39 changes: 18 additions & 21 deletions apps/system/sys_dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,24 @@
#include "app.h"
#include <string.h>

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");
Expand Down Expand Up @@ -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;
}
4 changes: 1 addition & 3 deletions apps/system/sys_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
#include "file.h"
#include <string.h>

block_if fs;

int main() {
SUCCESS("Enter kernel process GPID_FILE");

/* Initialize the file system */
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];
Expand Down
22 changes: 7 additions & 15 deletions apps/system/sys_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,12 @@
#include "disk.h"
#include <string.h>

/* 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];
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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;
}

Expand All @@ -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);
}
12 changes: 6 additions & 6 deletions grass/grass.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()");
}
Expand All @@ -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;
}
7 changes: 0 additions & 7 deletions grass/grass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions library/egos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit f054905

Please sign in to comment.