From 8567434231c7aa05ead621377375f46793ea767b Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Fri, 15 Nov 2024 17:20:51 +0000 Subject: [PATCH] Use a single `exit_status` instead of two --- src/threads/thread.c | 4 +--- src/threads/thread.h | 2 -- src/userprog/process.c | 23 +++++++++-------------- src/userprog/syscall.c | 4 ++-- 4 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/threads/thread.c b/src/threads/thread.c index e80f1e5..90a2a8b 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -673,7 +673,7 @@ init_process_result (struct thread *t) { struct process_result *result = malloc (sizeof (struct process_result)); result->tid = t->tid; - result->exit_status = t->exit_status; + result->exit_status = -1; lock_init (&result->lock); sema_init (&result->sema, 0); t->result = result; @@ -707,8 +707,6 @@ init_thread (struct thread *t, const char *name, int nice, int priority, t->recent_cpu = recent_cpu; t->priority = t->base_priority; - t->exit_status = -1; - old_level = intr_disable (); list_push_back (&all_list, &t->allelem); intr_set_level (old_level); diff --git a/src/threads/thread.h b/src/threads/thread.h index dfd6f0d..4a88577 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -135,8 +135,6 @@ struct thread /* Shared between thread.c and synch.c. */ struct list_elem elem; /* List element. */ - int exit_status; /* Exit Status: 0 = successful exit. */ - #ifdef USERPROG /* Owned by userprog/process.c. */ uint32_t *pagedir; /* Page directory. */ diff --git a/src/userprog/process.c b/src/userprog/process.c index 0e062b0..1f56875 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -56,7 +56,7 @@ struct process_start_data }; static thread_func start_process NO_RETURN; -static void destruct_process_result (struct hash_elem *e, void *aux); +static void destruct_process_result (struct hash_elem *e, void *aux UNUSED); static bool load (const char *cmdline, void (**eip) (void), void **esp); /* Starts a new thread running a user program executed via @@ -355,8 +355,6 @@ process_exit (void) struct thread *cur = thread_current (); uint32_t *pd; - printf ("%s: exit(%d)\n", cur->name, cur->exit_status); - /* Clean up all open files */ hash_destroy (&cur->open_files, fd_cleanup); @@ -369,9 +367,12 @@ process_exit (void) lock_release (&filesys_lock); } - /* Update own process result. */ if (cur->result != NULL) - destruct_process_result (&cur->result->elem, cur); + { + printf ("%s: exit(%d)\n", cur->name, cur->result->exit_status); + /* Update own process result. */ + destruct_process_result (&cur->result->elem, cur); + } /* Free child process results or signal parent's death. */ hash_destroy (&cur->child_results, destruct_process_result); @@ -394,16 +395,12 @@ process_exit (void) } } -/* Destruct a process_result, with multi-thread awareness. Takes the - process_result->elem and current thread. +/* Destruct a process_result, with multi-thread awareness. If the other thread is running, simply signals death. Otherwise - frees the result. - Also set's process_result->exit_status if the process result is FOR - the current thread. */ + frees the result. */ static void -destruct_process_result (struct hash_elem *e, void *cur_) +destruct_process_result (struct hash_elem *e, void *aux UNUSED) { - struct thread *cur = cur_; struct process_result *result = hash_entry (e, struct process_result, elem); lock_acquire (&result->lock); /* Other thread has died (and was not waited for). Free the result. */ @@ -415,8 +412,6 @@ destruct_process_result (struct hash_elem *e, void *cur_) /* Other thread is still alive, signal via sema that parent has died. */ else { - if (cur->tid == result->tid) - result->exit_status = cur->exit_status; sema_up (&result->sema); lock_release (&result->lock); } diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index 6beac7d..2f07d1b 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -131,7 +131,7 @@ syscall_exit (int status) { /* Sets exit_status of the thread to status. thread_exit () will call process_exit () if user programs are allowed. */ - thread_current ()->exit_status = status; + thread_current ()->result->exit_status = status; thread_exit (); } @@ -271,7 +271,7 @@ syscall_read (int fd, void *buffer, unsigned size) { /* Reading from the console. */ char *write_buffer = buffer; - for (int i = 0; i < size; i++) + for (unsigned i = 0; i < size; i++) write_buffer[i] = input_getc (); /* In case of console, read is always (eventually) successful. So return -- 2.49.1