From 882185145944e90e5a637c2c4023850208e80a8a Mon Sep 17 00:00:00 2001 From: Themis Demetriades Date: Thu, 14 Nov 2024 14:42:26 +0000 Subject: [PATCH] Refactor process_execute to remove use of 'goto' --- src/userprog/process.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/userprog/process.c b/src/userprog/process.c index ddd1997..9f404c7 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -67,9 +67,6 @@ process_execute (const char *cmd) struct process_start_data data; - sema_init (&data.loaded, 0); - data.success = false; - /* Make a copy of command. Otherwise there's a race between the caller and load(). */ cmd_copy = palloc_get_page (0); @@ -99,6 +96,8 @@ process_execute (const char *cmd) arguments. For details of arguments, see 'start_process'. */ data.cmd = cmd_copy; strlcpy (data.file_name, file_name, FNAME_MAX_LEN + 1); + sema_init (&data.loaded, 0); + data.success = false; tid = thread_create (file_name, PRI_DEFAULT, start_process, &data); if (tid == TID_ERROR) @@ -126,7 +125,6 @@ static void start_process (void *proc_start_data) { struct intr_frame if_; - bool success; struct process_start_data *data = proc_start_data; @@ -143,34 +141,36 @@ start_process (void *proc_start_data) if (exec_file == NULL) { lock_release (&filesys_lock); - goto fail; + sema_up (&data->loaded); + thread_exit (); } thread_current ()->exec_file = exec_file; file_deny_write (exec_file); lock_release (&filesys_lock); - success = load (data->file_name, &if_.eip, &if_.esp); + data->success = load (data->file_name, &if_.eip, &if_.esp); /* If load failed, quit. */ - if (!success) + if (!data->success) { palloc_free_page (data->cmd); - goto fail; + sema_up (&data->loaded); + thread_exit (); } /* Initialize user process stack and free page used to store the command that executed the process. */ - success = process_init_stack (data->cmd_saveptr, &if_.esp, data->file_name); + data->success = process_init_stack (data->cmd_saveptr, &if_.esp, data->file_name); + bool success = data->success; palloc_free_page (data->cmd); + sema_up (&data->loaded); /* If stack initialization failed, free resources and quit. */ if (!success) { - goto fail; + thread_exit (); } - data->success = true; - sema_up (&data->loaded); /* Start the user process by simulating a return from an interrupt, implemented by intr_exit (in threads/intr-stubs.S). Because intr_exit takes all of its @@ -179,12 +179,6 @@ start_process (void *proc_start_data) and jump to it. */ asm volatile ("movl %0, %%esp; jmp intr_exit" : : "g" (&if_) : "memory"); NOT_REACHED (); - - /* If starting the process failed, exit. */ -fail: - data->success = false; - sema_up (&data->loaded); - thread_exit (); } /* Helper function that initializes the stack of a newly created