hw3: process_wait implemented

This commit is contained in:
Claudio Maggioni 2020-04-26 17:42:05 +02:00
parent b7b1051190
commit 4e5c2e960d
3 changed files with 31 additions and 5 deletions

View file

@ -426,8 +426,10 @@ thread_exit (void)
and schedule another process. That process will destroy us and schedule another process. That process will destroy us
when it calls thread_schedule_tail(). */ when it calls thread_schedule_tail(). */
intr_disable (); intr_disable ();
list_remove (&thread_current()->allelem); struct thread* t = thread_current();
list_remove (&t->allelem);
thread_current ()->status = THREAD_DYING; thread_current ()->status = THREAD_DYING;
sema_up(&t->waiting_sem);
schedule (); schedule ();
NOT_REACHED (); NOT_REACHED ();
} }
@ -572,6 +574,7 @@ init_thread (struct thread *t, const char *name, int priority)
t->status = THREAD_BLOCKED; t->status = THREAD_BLOCKED;
strlcpy (t->name, name, sizeof t->name); strlcpy (t->name, name, sizeof t->name);
t->stack = (uint8_t *) t + PGSIZE; t->stack = (uint8_t *) t + PGSIZE;
sema_init(&t->waiting_sem, 0);
if (thread_mlfqs) { if (thread_mlfqs) {
t->nice_init = priority > 20 || priority < 20 ? 0 : priority; t->nice_init = priority > 20 || priority < 20 ? 0 : priority;
@ -710,6 +713,21 @@ schedule (void)
thread_schedule_tail (prev); 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. */ /* Returns a tid to use for a new thread. */
static tid_t static tid_t
allocate_tid (void) allocate_tid (void)

View file

@ -8,6 +8,7 @@
#include <list.h> #include <list.h>
#include <stdint.h> #include <stdint.h>
#include "threads/fpr_arith.h" #include "threads/fpr_arith.h"
#include "threads/synch.h"
/* States in a thread's life cycle. */ /* States in a thread's life cycle. */
enum thread_status enum thread_status
@ -99,6 +100,7 @@ struct thread
int nice; int nice;
int nice_init; int nice_init;
FPReal recent_cpu; FPReal recent_cpu;
struct semaphore waiting_sem;
#ifdef USERPROG #ifdef USERPROG
/* Owned by userprog/process.c. */ /* Owned by userprog/process.c. */
@ -147,5 +149,5 @@ int thread_get_nice (void);
void thread_set_nice (int); void thread_set_nice (int);
int thread_get_recent_cpu (void); int thread_get_recent_cpu (void);
int thread_get_load_avg (void); int thread_get_load_avg (void);
struct thread* thread_get_by_tid(tid_t);
#endif /* threads/thread.h */ #endif /* threads/thread.h */

View file

@ -86,9 +86,15 @@ start_process (void *file_name_)
This function will be implemented in problem 2-2. For now, it This function will be implemented in problem 2-2. For now, it
does nothing. */ does nothing. */
int int
process_wait (tid_t child_tid UNUSED) process_wait (tid_t child_tid)
{ {
struct thread* child = thread_get_by_tid(child_tid);
if (child == NULL) {
return -1; return -1;
}
sema_down(&child->waiting_sem);
return 0;
} }
/* Free the current process's resources. */ /* Free the current process's resources. */