From 4e5c2e960dedd5eae492f186beb059f9201f436a Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Sun, 26 Apr 2020 17:42:05 +0200 Subject: [PATCH] hw3: process_wait implemented --- pintos-env/pintos/threads/thread.c | 20 +++++++++++++++++++- pintos-env/pintos/threads/thread.h | 6 ++++-- pintos-env/pintos/userprog/process.c | 10 ++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pintos-env/pintos/threads/thread.c b/pintos-env/pintos/threads/thread.c index f8ba0f2..62424df 100644 --- a/pintos-env/pintos/threads/thread.c +++ b/pintos-env/pintos/threads/thread.c @@ -426,8 +426,10 @@ thread_exit (void) and schedule another process. That process will destroy us when it calls thread_schedule_tail(). */ intr_disable (); - list_remove (&thread_current()->allelem); + struct thread* t = thread_current(); + list_remove (&t->allelem); thread_current ()->status = THREAD_DYING; + sema_up(&t->waiting_sem); schedule (); NOT_REACHED (); } @@ -572,6 +574,7 @@ init_thread (struct thread *t, const char *name, int priority) t->status = THREAD_BLOCKED; strlcpy (t->name, name, sizeof t->name); t->stack = (uint8_t *) t + PGSIZE; + sema_init(&t->waiting_sem, 0); if (thread_mlfqs) { t->nice_init = priority > 20 || priority < 20 ? 0 : priority; @@ -710,6 +713,21 @@ schedule (void) thread_schedule_tail (prev); } +struct thread * +thread_get_by_tid (tid_t tid) +{ + struct list_elem *e; + for (e = list_begin (&all_list); + e != list_end (&all_list); + e = list_next (e)) + { + struct thread *t = list_entry (e, struct thread, allelem); + if (t->tid == tid) + return t; + } + return NULL; +} + /* Returns a tid to use for a new thread. */ static tid_t allocate_tid (void) diff --git a/pintos-env/pintos/threads/thread.h b/pintos-env/pintos/threads/thread.h index a37b265..dcebaaf 100755 --- a/pintos-env/pintos/threads/thread.h +++ b/pintos-env/pintos/threads/thread.h @@ -8,6 +8,7 @@ #include #include #include "threads/fpr_arith.h" +#include "threads/synch.h" /* States in a thread's life cycle. */ enum thread_status @@ -99,6 +100,7 @@ struct thread int nice; int nice_init; FPReal recent_cpu; + struct semaphore waiting_sem; #ifdef USERPROG /* Owned by userprog/process.c. */ @@ -147,5 +149,5 @@ int thread_get_nice (void); void thread_set_nice (int); int thread_get_recent_cpu (void); int thread_get_load_avg (void); - -#endif /* threads/thread.h */ +struct thread* thread_get_by_tid(tid_t); +#endif /* threads/thread.h */ \ No newline at end of file diff --git a/pintos-env/pintos/userprog/process.c b/pintos-env/pintos/userprog/process.c index c0e5215..92990d1 100755 --- a/pintos-env/pintos/userprog/process.c +++ b/pintos-env/pintos/userprog/process.c @@ -86,9 +86,15 @@ start_process (void *file_name_) This function will be implemented in problem 2-2. For now, it does nothing. */ int -process_wait (tid_t child_tid UNUSED) +process_wait (tid_t child_tid) { - return -1; + struct thread* child = thread_get_by_tid(child_tid); + if (child == NULL) { + return -1; + } + + sema_down(&child->waiting_sem); + return 0; } /* Free the current process's resources. */