Implement process_wait.
This commit is contained in:
@@ -36,9 +36,8 @@ struct process_result
|
|||||||
{
|
{
|
||||||
tid_t tid; /* The tid of the child process. */
|
tid_t tid; /* The tid of the child process. */
|
||||||
int exit_status; /* The exit status of the child process. Initially set to
|
int exit_status; /* The exit status of the child process. Initially set to
|
||||||
-1, then to 0 when parent dies, or to exit_status when
|
-1, then to exit_status when child dies. */
|
||||||
child dies (whichever happens first). */
|
struct lock lock; /* Lock to synchronise access to the status and sema. */
|
||||||
struct lock lock; /* Lock to synchronise access to the exit_status. */
|
|
||||||
struct semaphore sema; /* Semaphore to signal the parent that the exit_status
|
struct semaphore sema; /* Semaphore to signal the parent that the exit_status
|
||||||
has been set. */
|
has been set. */
|
||||||
struct list_elem elem; /* List element for the parent's children list. */
|
struct list_elem elem; /* List element for the parent's children list. */
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "userprog/process.h"
|
#include "userprog/process.h"
|
||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <list.h>
|
||||||
#include <round.h>
|
#include <round.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -202,12 +203,34 @@ start_process (void *file_name_)
|
|||||||
int
|
int
|
||||||
process_wait (tid_t child_tid UNUSED)
|
process_wait (tid_t child_tid UNUSED)
|
||||||
{
|
{
|
||||||
/* As a temporary wait, waiting will just put the thread to sleep for one
|
struct process_result *child_result = NULL;
|
||||||
second (TIMER_FREQ = 100 ticks ~ 1 second). */
|
struct list_elem *e;
|
||||||
/* TODO: Implement process_wait () correctly. Remove the next line. */
|
struct thread *cur = thread_current ();
|
||||||
timer_sleep (TIMER_FREQ);
|
for (e = list_begin (&cur->child_results);
|
||||||
|
e != list_end (&cur->child_results); e = list_next (e))
|
||||||
return 0; /* TODO: Change this too */
|
{
|
||||||
|
struct process_result *result
|
||||||
|
= list_entry (e, struct process_result, elem);
|
||||||
|
if (result->tid == child_tid)
|
||||||
|
{
|
||||||
|
child_result = result;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* List is ordered, allowing us to break early. */
|
||||||
|
else if (result->tid > child_tid)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (child_result == NULL)
|
||||||
|
return -1;
|
||||||
|
/* Wait for child to die. */
|
||||||
|
sema_down (&child_result->sema);
|
||||||
|
/* To prevent waiting for child twice, remove it from the list.
|
||||||
|
No need to use lock since this is the only thread with access to
|
||||||
|
the struct process_result now. */
|
||||||
|
list_remove (&child_result->elem);
|
||||||
|
int exit_status = child_result->exit_status;
|
||||||
|
free (child_result);
|
||||||
|
return exit_status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Free the current process's resources. */
|
/* Free the current process's resources. */
|
||||||
@@ -219,6 +242,47 @@ process_exit (void)
|
|||||||
|
|
||||||
printf ("%s: exit(%d)\n", cur->name, cur->exit_status);
|
printf ("%s: exit(%d)\n", cur->name, cur->exit_status);
|
||||||
|
|
||||||
|
/* Update process result. */
|
||||||
|
if (cur->result != NULL)
|
||||||
|
{
|
||||||
|
lock_acquire (&cur->result->lock);
|
||||||
|
cur->result->exit_status = cur->exit_status;
|
||||||
|
/* Parent has died, child has to free the struct process_result * */
|
||||||
|
if (sema_try_down (&cur->result->sema))
|
||||||
|
{
|
||||||
|
lock_release (&cur->result->lock);
|
||||||
|
free (cur->result);
|
||||||
|
}
|
||||||
|
/* Parent is still alive and will be the one to free the
|
||||||
|
struct process_result *, and may be waiting so call sema_up */
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sema_up (&cur->result->sema);
|
||||||
|
lock_release (&cur->result->lock);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct list_elem *e;
|
||||||
|
for (e = list_begin (&cur->child_results);
|
||||||
|
e != list_end (&cur->child_results); e = list_next (e))
|
||||||
|
{
|
||||||
|
struct process_result *result
|
||||||
|
= list_entry (e, struct process_result, elem);
|
||||||
|
lock_acquire (&result->lock);
|
||||||
|
/* Child has died (and was not waited for). Free the result. */
|
||||||
|
if (sema_try_down (&result->sema))
|
||||||
|
{
|
||||||
|
lock_release (&result->lock);
|
||||||
|
free (result);
|
||||||
|
}
|
||||||
|
/* Child is still alive, signal via sema that parent has died. */
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sema_up (&result->sema);
|
||||||
|
lock_release (&result->lock);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Destroy the current process's page directory and switch back
|
/* Destroy the current process's page directory and switch back
|
||||||
to the kernel-only page directory. */
|
to the kernel-only page directory. */
|
||||||
pd = cur->pagedir;
|
pd = cur->pagedir;
|
||||||
|
|||||||
Reference in New Issue
Block a user