Skip to content

Commit

Permalink
implemented ps
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Gu committed Aug 18, 2023
1 parent 3a3d8b3 commit 6cd16af
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 5 deletions.
14 changes: 14 additions & 0 deletions apps/user/loop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "app.h"
#include <stdlib.h>

// The same as clock.c, except this one does not print
int main(int argc, char** argv) {
int cnt = (argc == 1)? 1000 : atoi(argv[1]);

for (int i = 0; i < cnt; i++) {
for (int j = 0; j < 5000000; j++);
//printf("clock: tick#%d / #%d\r\n", i + 1, cnt);
}

return 0;
}
8 changes: 8 additions & 0 deletions apps/user/ps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "app.h"
#include <stdlib.h>

// Shows process status
int main(int argc, char** argv) {
grass->ps();
return 0;
}
6 changes: 6 additions & 0 deletions apps/user/ult.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,15 @@ void test_code(void *arg) {

int main() {
INFO("User-level threading is not implemented.");

// Threading test
/*thread_init();
thread_create(test_code, "thread 1", 16 * 1024);
thread_create(test_code, "thread 2", 16 * 1024);
test_code("main thread");
thread_exit();*/

// Basic consumer producer test
thread_init();
sema_init(&s_full, 0);
sema_init(&s_empty, NSLOTS);
Expand All @@ -313,6 +316,9 @@ int main() {
thread_create(producer, "producer 3", 16 * 1024);
producer("producer 1");
thread_exit();



INFO("main function returns 0");
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions grass/grass.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ int main() {
grass->proc_alloc = proc_alloc;
grass->proc_free = proc_free;
grass->proc_set_ready = proc_set_ready;
grass->ps = ps;

grass->sys_exit = sys_exit;
grass->sys_send = sys_send;
Expand Down
35 changes: 33 additions & 2 deletions grass/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

void intr_entry(int id);

void excp_entry(int id) {
void excp_entry(int id) { // HW 3
/* Student's code goes here (handle memory exception). */

/* If id is for system call, handle the system call and return */
Expand All @@ -26,7 +26,7 @@ void excp_entry(int id) {
FATAL("excp_entry: kernel got exception %d", id);
}

void proc_init() {
void proc_init() { // HW 3
earth->intr_register(intr_entry);
earth->excp_register(excp_entry);

Expand Down Expand Up @@ -63,6 +63,37 @@ int proc_alloc() {
FATAL("proc_alloc: reach the limit of %d processes", MAX_NPROCESS);
}

void ps() {
for (int i = 0; i < MAX_NPROCESS; i++)
if (proc_set[i].pid > 0) {
printf("%d ", proc_set[i].pid);
int status = proc_set[i].status;
switch(status) {
case PROC_LOADING:
printf("PROC_LOADING");
break;
case PROC_READY:
printf("PROC_READY");
break;
case PROC_RUNNABLE:
printf("PROC_RUNNABLE");
break;
case PROC_RUNNING:
printf("PROC_RUNNING");
break;
case PROC_UNUSED:
printf("PROC_UNUSED");
break;
case PROC_WAIT_TO_RECV:
printf("PROC_WAIT_TO_RECV");
break;
case PROC_WAIT_TO_SEND:
printf("PROC_WAIT_TO_SEND");
}
printf("\n");
}
}

void proc_free(int pid) {
if (pid != -1) {
earth->mmu_free(pid);
Expand Down
1 change: 1 addition & 0 deletions grass/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void timer_reset();

void proc_init();
int proc_alloc();
void ps();
void proc_free(int);
void proc_set_ready (int);
void proc_set_running (int);
Expand Down
2 changes: 1 addition & 1 deletion grass/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void intr_entry(int id) {
ctx_start(&proc_set[proc_curr_idx].sp, (void*)GRASS_STACK_TOP);
}

void ctx_entry() {
void ctx_entry() { // HW 3?
/* Now on the kernel stack */
int mepc, tmp;
asm("csrr %0, mepc" : "=r"(mepc));
Expand Down
1 change: 1 addition & 0 deletions library/egos.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct grass {

/* Process control interface */
int (*proc_alloc)();
void (*ps)();
void (*proc_free)(int pid);
void (*proc_set_ready)(int pid);

Expand Down
6 changes: 4 additions & 2 deletions tools/mkfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ char* kernel_processes[] = {
#8: /bin/cat #9: /bin/ls #10:/bin/cd #11:/bin/pwd
#12:/bin/clock #13:/bin/crash1 #14:/bin/crash2 #15:/bin/ult
*/
#define NINODE 16
#define NINODE 18
char* contents[] = {
"./ 0 ../ 0 home/ 1 bin/ 6 ",
"./ 1 ../ 0 yunhao/ 2 rvr/ 3 lorenzo/ 4 ",
"./ 2 ../ 1 README 5 ",
"./ 3 ../ 1 ",
"./ 4 ../ 1 ",
"With only 2000 lines of code, egos-2000 implements boot loader, microSD driver, tty driver, memory paging, address translation, interrupt handling, process scheduling and messaging, system call, file system, shell, 7 user commands and the `mkfs/mkrom` tools.",
"./ 6 ../ 0 echo 7 cat 8 ls 9 cd 10 pwd 11 clock 12 crash1 13 crash2 14 ult 15 ",
"./ 6 ../ 0 echo 7 cat 8 ls 9 cd 10 pwd 11 clock 12 crash1 13 crash2 14 ult 15 loop 16 ps 17",
"#../build/release/echo.elf",
"#../build/release/cat.elf",
"#../build/release/ls.elf",
Expand All @@ -55,6 +55,8 @@ char* contents[] = {
"#../build/release/crash1.elf",
"#../build/release/crash2.elf",
"#../build/release/ult.elf",
"#../build/release/loop.elf",
"#../build/release/ps.elf",
};

char fs[FS_DISK_SIZE], exec[GRASS_EXEC_SIZE];
Expand Down

0 comments on commit 6cd16af

Please sign in to comment.