diff --git a/src/userprog/process.c b/src/userprog/process.c index 9f404c7..65223c1 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -49,7 +49,7 @@ struct process_start_data be started. */ bool success; /* Indicates whether the process was successfully loaded. */ struct semaphore loaded; /* Semaphore used to signal that the process has - been loaded. */ + finished attempting to load. */ }; static thread_func start_process NO_RETURN; @@ -64,7 +64,6 @@ process_execute (const char *cmd) { char *cmd_copy; tid_t tid; - struct process_start_data data; /* Make a copy of command. @@ -81,11 +80,8 @@ process_execute (const char *cmd) of the process. */ char *file_name = strtok_r (cmd_copy, " ", &data.cmd_saveptr); - /* NOTE: Currently, the file being executed is closed in load () and then - reopened here. Because load is an exported public function, this - might be necessary. */ + /* Validates that the current file to be executed can be opened/exists. */ lock_acquire (&filesys_lock); - /* Validates that the current file to be executed is a valid file */ bool valid_file = filesys_open (file_name) != NULL; lock_release (&filesys_lock); if (!valid_file) @@ -100,14 +96,20 @@ process_execute (const char *cmd) data.success = false; tid = thread_create (file_name, PRI_DEFAULT, start_process, &data); + + /* Return TID_ERROR and free resources if starting execution went wrong. */ if (tid == TID_ERROR) palloc_free_page (cmd_copy); else { + + /* Wait until process file has finished attempting to load via the child + thread before reporting success of starting execution. */ sema_down (&data.loaded); if (!data.success) tid = TID_ERROR; } + return tid; } @@ -119,8 +121,11 @@ static void *push_to_stack (void **esp, void *data, size_t data_size); /* Make the current thread execute 'cmd', passing in a copy of the command string used for processing, the saveptr used by strtok_r (in order to further tokenize the same command and retrieve its - arguments), as well as the name of the file being executed. This - involves loading the specified file and starting it running. */ + arguments), the name of the file being executed, and a semaphore that + calls sema_up to indicate that the 'success' variable passed to it + has been updated to indicate whether the process file loading succeeded. + This involves loading the specified file and calling its main () function + with the specified command arguments. */ static void start_process (void *proc_start_data) {