From f4c900e56cc85273015ecc4cf562d4190a0ea087 Mon Sep 17 00:00:00 2001 From: sBubshait Date: Fri, 15 Nov 2024 14:37:38 +0000 Subject: [PATCH] Refactor process.c for comments, clarity and readability --- src/userprog/process.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/userprog/process.c b/src/userprog/process.c index afc39f4..ad0bdec 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -84,9 +84,9 @@ process_execute (const char *cmd) /* Validates that the current file to be executed can be opened/exists. */ lock_acquire (&filesys_lock); - bool valid_file = filesys_open (file_name) != NULL; + struct file *file = filesys_open (file_name); lock_release (&filesys_lock); - if (!valid_file) + if (file == NULL) return TID_ERROR; /* Create a new thread to execute the command, by initializing @@ -128,7 +128,6 @@ static void start_process (void *proc_start_data) { struct intr_frame if_; - struct process_start_data *data = proc_start_data; /* Initialize interrupt frame and load executable. */ @@ -136,41 +135,46 @@ start_process (void *proc_start_data) if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG; if_.cs = SEL_UCSEG; if_.eflags = FLAG_IF | FLAG_MBS; - + + /* Acquire the file system lock to prevent race conditions. */ lock_acquire (&filesys_lock); - - /* Prevent writing to the file being executed. */ + struct file *exec_file = filesys_open (data->file_name); if (exec_file == NULL) { + /* If the executable file cannot be opened, free resources and quit. */ lock_release (&filesys_lock); sema_up (&data->loaded); thread_exit (); } + + /* Deny write to the executable file to prevent writing to it and release the + file system lock. */ file_deny_write (exec_file); lock_release (&filesys_lock); + thread_current ()->exec_file = exec_file; + /* Load the ELF executable file, and store the success of the operation in + the 'success' variable in data. */ data->success = load (data->file_name, &if_.eip, &if_.esp); - /* If load failed, quit. */ - if (!data->success) + /* If load was sucessful, initialize user process stack and free page used + to store the command that executed the process. */ + if (data->success) { - sema_up (&data->loaded); - thread_exit (); + data->success = + process_init_stack (data->cmd_saveptr, &if_.esp, data->file_name); } - /* Initialize user process stack and free page used to store the - command that executed the process. */ - data->success = process_init_stack (data->cmd_saveptr, &if_.esp, data->file_name); + /* Signal that the process has finished attempting to load. */ bool success = data->success; sema_up (&data->loaded); - /* If stack initialization failed, free resources and quit. */ + /* If the load was unsuccessful or if it was but the stack initialization + failed, exit the thread. */ if (!success) - { thread_exit (); - } /* Start the user process by simulating a return from an interrupt, implemented by intr_exit (in