diff --git a/student-distrib/bootimg b/student-distrib/bootimg index 98e51374322d4c0b555858e7cac9d72f4af9f2eb..4a464ceb3fc8176e22145112ad23995764e3b49c 100755 Binary files a/student-distrib/bootimg and b/student-distrib/bootimg differ diff --git a/student-distrib/mp3.img b/student-distrib/mp3.img index 338fe28b1dbc4833a522aed2c44009ee5df03e9f..0521e3fd466ab5e2a974b840b1a2c89b0d2a82b1 100755 Binary files a/student-distrib/mp3.img and b/student-distrib/mp3.img differ diff --git a/student-distrib/proc.c b/student-distrib/proc.c index 4e7766a820c9121fe8dba465f5eff08ee68c8c26..b8792dc2aa369eff0fe7989d0027e2ff4765c22d 100644 --- a/student-distrib/proc.c +++ b/student-distrib/proc.c @@ -1,6 +1,6 @@ #include "proc.h" -/* File operations table holding function pointers */ +/* File operations table initialized to hold function pointers */ f_table_t dir_jumptable = { open_dir, close_dir, read_dir, write_dir }; f_table_t file_jumptable = { open_file, close_file, read_file, write_file }; f_table_t rtc_jumptable = { rtc_open, rtc_close, rtc_read, rtc_write }; @@ -8,18 +8,29 @@ f_table_t stdin_jumptable = { terminal_open, terminal_close, terminal_read, f_table_t stdout_jumptable = { terminal_open, terminal_close, terminal_read, terminal_write }; f_table_t no_jumptable = { fail , fail , fail , fail }; + +/* The fail function + * + * DESCRIPTION: This function is a place holder + * + * RETURN: always return -1 + */ int32_t fail(){ return -1; } -/* Initialized at bootup */ +/* The control initilization function + * + * DESCRIPTION: This function initialize all global variable + * + * RETURN: none + */ void control_initilization(void) { int i; /* All PID entries to present */ - for(i = 0; i < MAX_PROCESS; i++) - pid_array[i] = 0; + for(i = 0; i < MAX_PROCESS; i++) pid_array[i] = 0; total_prog_count = 0; visible_terminal = 0; @@ -28,26 +39,39 @@ void control_initilization(void) counter = 0; } -/* Checks if FD is valid - * Returns -1 for invalid FD - * Returns 0 for valid FD */ +/* The file descriptor table checking function + * + * DESCRIPTION: This function Checks if FD is valid + * + * INPUT: fd -- the slot on fd table that we want to checks + * + * RETURN: -1 if fd is out of range or the file is not present + * 0 if the file is present + */ int32_t fd_check(int32_t fd) { /* First check for valid index */ if (fd >= MIN_FILE_NUM && fd < MAX_FILE_NUM ) { - /* Now check if file is present */ + /* Now check if file is present */ // // use running_terminal instead of visible_terminal !!! - if ( terminal[running_terminal].pcb->fd[fd].flags != 0) - return 0; + if ( terminal[running_terminal].pcb->fd[fd].flags != 0) + return 0; } return -1; } -/* Clear FD for process on halt */ +/* The file descriptor table clearing function + * + * DESCRIPTION: This function clears a fd table slot + * this function is called when halt + * + * INPUT: fd -- the slot on fd table that we want to clears + * + * RETURN: none + */ void fd_clear(int32_t fd) { - // use running_terminal instead of visible_terminal !!! terminal[running_terminal].pcb->fd[fd].fileop_ptr.read = 0; terminal[running_terminal].pcb->fd[fd].fileop_ptr.write = 0; @@ -56,11 +80,22 @@ void fd_clear(int32_t fd) terminal[running_terminal].pcb->fd[fd].inode_index = 0; terminal[running_terminal].pcb->fd[fd].file_pos = 0; terminal[running_terminal].pcb->fd[fd].flags = 0; - return; } -/* Initialize PCB for new process on execute */ +/* The PCB initilization function + * + * DESCRIPTION: Initialize PCB for new process on execute + * This function is called when executed + * + * INPUT: pcb -- pcb pointer + * parsed_cmd -- command + * argv -- argument + * pid -- process id + * arg_num -- # of arguments + * + * RETURN: none + */ void pcb_init(pcb_t* pcb , uint8_t* parsed_cmd, uint8_t (*argv)[MAX_ARGUMENT_SIZE], uint32_t pid, uint32_t arg_num) { int i; @@ -113,83 +148,93 @@ void pcb_init(pcb_t* pcb , uint8_t* parsed_cmd, uint8_t (*argv)[MAX_ARGUMENT_SIZ return; } -/* Parsing the command, extracting the command and the argument - * returns number of arguments when successful, -2 fot exit, -1 for failure */ +/* The command parsing function + * + * DESCRIPTION: This function parsing the command, extracting the command and the argument + * + * INPUT: command -- input command + * parsed_cmd -- output command + * argv -- output argument + * + * RETURN: number of arguments when successful, + * -2 fot exit, + * -1 for failure. + */ int32_t parse_cmd(const uint8_t* command, uint8_t* parsed_cmd, uint8_t (*argv)[MAX_ARGUMENT_SIZE]) { int32_t i,j, command_length; int32_t end = 0; int32_t start = 0; - //clear parsed_cmd buffer + /* clear parsed_cmd buffer */ for(i = 0; i < MAX_COMMAND_SIZE; i++) parsed_cmd[i] = '\0'; - //clear argument buffer + /* clear argument buffer */ for(i = 0; i < MAX_ARGUMENT_NUM; i++) { - for(j = 0; j < MAX_ARGUMENT_SIZE; j++) + for(j = 0; j < MAX_ARGUMENT_SIZE; j++) argv[i][j] = '\0'; } - //skip spaces at start of command + /* skip spaces at start of command */ while (command[start] == ' ') { start++; end++; } - //find end of command (end in space or line terminator) + /* find end of command (end in space or line terminator) */ while (command[end] != ' ' && command[end] != '\0' && command[end] != AS_NL) end++; - //check if command is bigger than valid command size + /* check if command is bigger than valid command size */ if(end - start > MAX_COMMAND_SIZE) { printf("command name exceeded the maximum size of %d\n", MAX_COMMAND_SIZE); return -1; } - //copy command to parsed_cmd + /* copy command to parsed_cmd */ for (i = 0; i < (end - start); i++) if(command[i+start] != 0x0D) //check for \n parsed_cmd[i] = command[i+start]; command_length = strlen((int8_t*)command); - //skip spaces (needs to be here for return value) + /* skip spaces (needs to be here for return value) */ while(command[end] == ' ') end++; - //extracting arguments + /* extracting arguments */ for( j = 0 ; end < command_length ; j++ ) { - //set new start to end of last argument + /* set new start to end of last argument */ start = end; - //check if max number of arguments is reached + /* check if max number of arguments is reached */ if(j >= MAX_ARGUMENT_NUM) { printf("input exceeded the maximum number of arguments %d\n", MAX_ARGUMENT_NUM); return -1; } - //skip spaces until argument is reached + /* skip spaces until argument is reached */ while (command[start] == ' ') { start++; end++; } - //find end of command (end in space or line terminator) + /* find end of command (end in space or line terminator) */ while (command[end] != ' ' && command[end] != '\0' && command[end] != AS_NL) end++; - //check if argument is bigger than valid command size + /* check if argument is bigger than valid command size */ if(end - start > MAX_ARGUMENT_SIZE) { printf("argument %d exceeded the maximum size of %d\n", j+1, MAX_ARGUMENT_SIZE); return -1; } - //copy argument to return array + /* copy argument to return array */ for (i = 0; i < (end - start); i++) if(command[i+start] != 0x0D) //check for \n argv[j][i] = command[i+start]; @@ -205,15 +250,25 @@ int32_t parse_cmd(const uint8_t* command, uint8_t* parsed_cmd, uint8_t (*argv)[M return j; } +/* The exit function + * + * DESCRIPTION: This function exit a program + * + * RETURN: none + */ void exit(void) { asm volatile( "call halt;"); return; } -/* Determines the next available process number */ -/* 0--0--0--0--0--0 */ -/* Returns available PID 0-5 */ +/* The set_pidfunction + * + * DESCRIPTION: This function finds an avalible pid in pid array + * + * RETURN: avalibel pid hen succesed + * -1 when all the slot are occupied + */ int32_t set_pid(void) { int32_t i; @@ -230,19 +285,41 @@ int32_t set_pid(void) return -1; } -int32_t get_pid(void) +/* The count_pid function + * + * DESCRIPTION: This function counts active pid + * + * RETURN: # of active pid + */ +int32_t count_pid(void) { int32_t i, count = -1; for (i = 0; i < MAX_PROCESS; i++){ if ( pid_array[i] == 1) count++; } return count; } +/* The clear_pid function + * + * DESCRIPTION: This function clears a pid in pid array + * + * INPUT: pid -- the pid that we want to clear + * + * RETURN: # of active pid + */ int32_t clear_pid(int32_t pid) { - pid_array[pid] = 0; - return get_pid(); + pid_array[pid] = 0; + return count_pid(); } +/* The get_pcb function + * + * DESCRIPTION: This function calculate the pcb pointer for a given pid + * + * INPUT: pid -- the pid that we want to get the pcb from + * + * RETURN: pcb pointer that points to the pcb of the input pid + */ pcb_t* get_pcb(uint32_t pid){ return (pcb_t *)(_8_MB - (pid + 1) * _8_KB); } diff --git a/student-distrib/proc.h b/student-distrib/proc.h index 2134b896e73a4c8371ab16f89ab85d476efd0d44..bd6628e99fa8c6cf095bb827c510a0b6c07ecc5a 100644 --- a/student-distrib/proc.h +++ b/student-distrib/proc.h @@ -23,7 +23,6 @@ extern pcb_t* current_pcb(void); extern pcb_t* get_pcb(uint32_t pid); extern int32_t parse_cmd(const uint8_t* command, uint8_t* cmd, uint8_t (*argv)[MAX_ARGUMENT_SIZE]); /* Function for parsing commands */ -extern int32_t get_pid(void); +extern int32_t count_pid(void); extern int32_t set_pid(void); extern int32_t clear_pid(int32_t pid); - diff --git a/student-distrib/scheduler.h b/student-distrib/scheduler.h index 5cf82c3b1e743404e67ee2e0023f347f859b0da4..52a3c62313531b737f04d60c594f6705fc6405bb 100644 --- a/student-distrib/scheduler.h +++ b/student-distrib/scheduler.h @@ -5,7 +5,6 @@ #include "syscall.h" #include "paging.h" -pcb_t* get_pcb(uint32_t pid); int32_t remap_proc(int32_t pid); extern void scheduler(void); diff --git a/student-distrib/syscall.c b/student-distrib/syscall.c index 7c6d150c5fde397426adce55c5fc6f04d98f3416..cdc4873d8d286bc32a18ef01ccf3d5c6bb63ea57 100644 --- a/student-distrib/syscall.c +++ b/student-distrib/syscall.c @@ -5,9 +5,9 @@ * int32_t halt (uint8_t status) * * DESCRIPTION: This function terminates a process, returning the specified value to its parent process. - * + * * INPUT: status - * + * * RETURN: always return 0 */ int32_t halt (uint8_t status) { @@ -102,7 +102,8 @@ int32_t execute(const uint8_t* command) uint8_t argv[MAX_ARGUMENT_NUM][MAX_ARGUMENT_SIZE]; int32_t ret, arg_num; //number that indicated special command - int32_t pid = get_pid(); + int32_t pid = set_pid(); + //if( pid == -1) return -1; // reach max process ret = parse_cmd(command, parsed_cmd, argv); @@ -141,7 +142,6 @@ int32_t execute(const uint8_t* command) } // Get a new pid - pid = set_pid(); total_prog_count++; terminal[running_terminal].terminal_prog_count++; @@ -371,10 +371,10 @@ int32_t open (const uint8_t* filename){ */ int32_t close (int32_t fd) { - + // invalid fd if(fd_check(fd) < 0 || fd < USER_FILE_START) - return -1; + return -1; terminal[running_terminal].pcb->fd[fd].fileop_ptr.close(fd); @@ -407,7 +407,7 @@ int32_t getargs (uint8_t* buf, int32_t nbytes){ //If there are no arguments if ( terminal[running_terminal].pcb->arg_num == 0 || nbytes <= 0 ) { - return -1; + return -1; } // if the arguments and a terminal NULL (0-byte) do not fit in the buffer. diff --git a/student-distrib/syscall.h b/student-distrib/syscall.h index fc157f154dd9df86019473d0f160551ae885a9bc..edf5b0c83f690c0958b01d94598b52875b766c9f 100644 --- a/student-distrib/syscall.h +++ b/student-distrib/syscall.h @@ -41,7 +41,6 @@ extern void remap_vidmem(); //from proc.h int32_t parse_cmd(const uint8_t* command, uint8_t* cmd, uint8_t (*argv)[MAX_ARGUMENT_SIZE]); /* Function for parsing commands */ -int32_t get_pid(void); int32_t set_pid(void); int32_t clear_pid(int32_t pid); int32_t fd_check(int32_t fd); /* File Descriptor helper functions */ diff --git a/student-distrib/types.h b/student-distrib/types.h index b69089fff0be8a1c0b6d3f05592c51341a1b5324..0c9c9dc1ca4c5bf9bf43ede7087e36000cc7dbf3 100644 --- a/student-distrib/types.h +++ b/student-distrib/types.h @@ -45,9 +45,9 @@ //special place in memory #define KERNEL_ADDR 0x00400000 -#define VIDEO_ADDR 0x000B8000 /* Shifting required to write to registers correctl */ -#define VIDEO_MEM 0xB8 /* Shifting required to write to registers correctly */ -#define STACK 0x8400000 /* 132 MB */ +#define VIDEO_ADDR 0x000B8000 /* Shifting required to write to registers correctl */ +#define VIDEO_MEM 0xB8 /* Shifting required to write to registers correctly */ +#define STACK 0x8400000 /* 132 MB */ #define TABLE_SIZE 4 #define USER_FILE_START 2 @@ -143,22 +143,22 @@ typedef struct pcb { file_object_t fd[MAX_FILE_NUM]; /* File descriptor array */ - int32_t scheduler_flag; int32_t shell_flag; + int32_t scheduler_flag; - uint32_t pid; /* Holds current process ID */ - uint32_t current_esp; //newly added, need initilization - uint32_t current_ebp; //newly added, need initilization - uint32_t current_eip; //newly added, need initilization + uint32_t pid; /* Holds current process ID */ + uint32_t current_esp; /* Store Current esp for scheduling */ + uint32_t current_ebp; /* Store Current ebp for scheduling */ + uint32_t current_eip; /* Store Current eip for scheduling */ struct pcb* parent_pcb; - uint32_t parent_pid; /* Holds parent process ID */ - uint32_t parent_esp; /* Stores Kernel ESP, EBP*/ - uint32_t parent_ebp; /* Stores Kernel ESP, EBP*/ + uint32_t parent_pid; /* Holds parent process ID */ + uint32_t parent_esp; /* Stores Kernel ESP, EBP*/ + uint32_t parent_ebp; /* Stores Kernel ESP, EBP*/ - uint32_t tss_esp0; /* saves TSS before context switch */ + uint32_t tss_esp0; /* saves TSS before context switch */ - uint32_t arg_num; /* Number of arguments in argv */ + uint32_t arg_num; /* Number of arguments in argv */ uint8_t cmd[MAX_COMMAND_SIZE]; /* Command */ uint8_t argv[MAX_ARGUMENT_NUM][MAX_ARGUMENT_SIZE]; /* Argument table */ @@ -181,30 +181,27 @@ volatile int32_t buffer_index; } terminal_t; -/*----------------------------- Control Structure ----------------------------*/ - - - terminal_t terminal[MAX_TERMINAL]; - pcb_t * curr_pcb; - int32_t visible_terminal; - int32_t running_terminal; - int32_t total_prog_count; - uint32_t filesys_start; - int32_t pid_array[MAX_PROCESS]; - int32_t counter; - - int32_t cur_pid; //newly added, need initilization - - /*---------------------------- Global variable -------------------------------*/ - -f_table_t dir_jumptable ; -f_table_t file_jumptable ; -f_table_t rtc_jumptable ; -f_table_t stdin_jumptable ; -f_table_t stdout_jumptable ; -f_table_t no_jumptable ; + uint32_t filesys_start; /* Starting place for file system */ + int32_t counter; /* */ + + terminal_t terminal[MAX_TERMINAL]; /* Terminal array that store the information for each termianl */ + int32_t visible_terminal; /* */ + int32_t running_terminal; /* */ + + int32_t pid_array[MAX_PROCESS]; /* PID array that store information for each pid */ + int32_t total_prog_count; /* Counter for the total running program */ + int32_t cur_pid; /* PID of Current running program */ + pcb_t * curr_pcb; /* PCB pointer of Current running program */ + + /* Function table for system call read/write/open/close */ + f_table_t dir_jumptable ; + f_table_t file_jumptable ; + f_table_t rtc_jumptable ; + f_table_t stdin_jumptable ; + f_table_t stdout_jumptable ; + f_table_t no_jumptable ; #endif /* ASM */