Implement process_wait #31
@@ -170,6 +170,9 @@ list_insert (struct list_elem *before, struct list_elem *elem)
|
||||
{
|
||||
ASSERT (is_interior (before) || is_tail (before));
|
||||
ASSERT (elem != NULL);
|
||||
// Sanity checks to prevent (some) loop lists
|
||||
ASSERT (before != elem);
|
||||
ASSERT (before->prev != elem);
|
||||
|
||||
elem->prev = before->prev;
|
||||
elem->next = before;
|
||||
|
||||
@@ -341,6 +341,7 @@ lock_release (struct lock *lock)
|
||||
released, transfer the remaining orphaned donors to its donor list. */
|
||||
if (max_donor != NULL)
|
||||
{
|
||||
list_remove (&max_donor->donor_elem);
|
||||
while (!list_empty (&orphan_list))
|
||||
list_push_back (&max_donor->donors_list, list_pop_front (&orphan_list));
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "threads/flags.h"
|
||||
#include "threads/interrupt.h"
|
||||
#include "threads/intr-stubs.h"
|
||||
#include "threads/malloc.h"
|
||||
#include "threads/palloc.h"
|
||||
#include "threads/switch.h"
|
||||
#include "threads/synch.h"
|
||||
@@ -68,6 +69,7 @@ static void kernel_thread (thread_func *, void *aux);
|
||||
static void idle (void *aux UNUSED);
|
||||
static struct thread *running_thread (void);
|
||||
static struct thread *next_thread_to_run (void);
|
||||
static void init_process_result (struct thread *t);
|
||||
static void init_thread (struct thread *, const char *name, int nice,
|
||||
int priority, fp32_t recent_cpu);
|
||||
static bool is_thread (struct thread *) UNUSED;
|
||||
@@ -110,6 +112,7 @@ thread_init (void)
|
||||
initial_thread_recent_cpu);
|
||||
initial_thread->status = THREAD_RUNNING;
|
||||
initial_thread->tid = allocate_tid ();
|
||||
initial_thread->result = NULL; /* Main thread cannot be waited for. */
|
||||
}
|
||||
|
||||
/* Starts preemptive thread scheduling by enabling interrupts.
|
||||
@@ -236,6 +239,7 @@ thread_create (const char *name, int priority,
|
||||
struct thread *parent_thread = thread_current ();
|
||||
init_thread (t, name, parent_thread->nice, priority, parent_thread->recent_cpu);
|
||||
tid = t->tid = allocate_tid ();
|
||||
init_process_result (t);
|
||||
|
||||
/* Prepare thread for first run by initializing its stack.
|
||||
Do this atomically so intermediate values for the 'stack'
|
||||
@@ -259,6 +263,10 @@ thread_create (const char *name, int priority,
|
||||
|
||||
intr_set_level (old_level);
|
||||
|
||||
/* No need to synchronise child_results since it is only ever accessed by one
|
||||
thread. By the nature of increasing TIDs, this list is ordered. */
|
||||
list_push_back (&parent_thread->child_results, &t->result->elem);
|
||||
|
||||
/* Add to run queue. */
|
||||
thread_unblock (t);
|
||||
|
||||
@@ -632,6 +640,18 @@ is_thread (struct thread *t)
|
||||
return t != NULL && t->magic == THREAD_MAGIC;
|
||||
}
|
||||
|
||||
/* Allocate and initialise a process result for given thread. */
|
||||
static void
|
||||
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;
|
||||
lock_init (&result->lock);
|
||||
sema_init (&result->sema, 0);
|
||||
t->result = result;
|
||||
}
|
||||
|
||||
/* Does basic initialization of T as a blocked thread named
|
||||
NAME. */
|
||||
static void
|
||||
@@ -660,6 +680,7 @@ init_thread (struct thread *t, const char *name, int nice, int priority,
|
||||
t->priority = t->base_priority;
|
||||
|
||||
t->exit_status = -1;
|
||||
list_init (&t->child_results);
|
||||
|
||||
old_level = intr_disable ();
|
||||
list_push_back (&all_list, &t->allelem);
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <debug.h>
|
||||
#include <list.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "threads/synch.h"
|
||||
#include "threads/fixed-point.h"
|
||||
|
||||
/* States in a thread's life cycle. */
|
||||
@@ -29,6 +31,18 @@ typedef int tid_t;
|
||||
#define NICE_DEFAULT 0 /* Default niceness. */
|
||||
#define NICE_MAX 20 /* Highest niceness. */
|
||||
|
||||
/* A process result, synchronised between parent and child. */
|
||||
struct process_result
|
||||
{
|
||||
tid_t tid; /* The tid of the child process. */
|
||||
int exit_status; /* The exit status of the child process. Initially set
|
||||
to -1, then to exit_status when child dies. */
|
||||
struct lock lock; /* Lock the exit_status and sema. */
|
||||
struct semaphore sema; /* Semaphore to signal the parent that the exit_status
|
||||
has been set. */
|
||||
struct list_elem elem; /* List element for the parent's children list. */
|
||||
};
|
||||
|
||||
/* A kernel thread or user process.
|
||||
|
||||
Each thread structure is stored in its own 4 kB page. The
|
||||
@@ -108,6 +122,11 @@ struct thread
|
||||
int nice; /* Nice value for this thread */
|
||||
fp32_t recent_cpu; /* Amount of time this process received */
|
||||
|
||||
/* Process wait properties. */
|
||||
struct process_result *result; /* Result of the process. */
|
||||
struct list child_results; /* List of children's of this thread
|
||||
process results. */
|
||||
|
||||
/* Shared between thread.c and synch.c. */
|
||||
struct list_elem elem; /* List element. */
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "userprog/process.h"
|
||||
#include <debug.h>
|
||||
#include <inttypes.h>
|
||||
#include <list.h>
|
||||
#include <round.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -202,12 +203,39 @@ start_process (void *file_name_)
|
||||
int
|
||||
process_wait (tid_t child_tid UNUSED)
|
||||
{
|
||||
/* As a temporary wait, waiting will just put the thread to sleep for one
|
||||
second (TIMER_FREQ = 100 ticks ~ 1 second). */
|
||||
/* TODO: Implement process_wait () correctly. Remove the next line. */
|
||||
timer_sleep (TIMER_FREQ);
|
||||
|
||||
return 0; /* TODO: Change this too */
|
||||
struct process_result *child_result = NULL;
|
||||
struct list_elem *e;
|
||||
struct thread *cur = thread_current ();
|
||||
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);
|
||||
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);
|
||||
/* We need lock release in process_exit, so we need to acquire (and possibly
|
||||
wait) for it here to ensure we don't free the lock memory before it is
|
||||
released in process_exit. */
|
||||
lock_acquire (&child_result->lock);
|
||||
/* 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;
|
||||
lock_release (&child_result->lock);
|
||||
free (child_result);
|
||||
return exit_status;
|
||||
}
|
||||
|
||||
/* Free the current process's resources. */
|
||||
@@ -219,6 +247,48 @@ process_exit (void)
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/* Free child process results or signal parent's death. */
|
||||
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
|
||||
to the kernel-only page directory. */
|
||||
pd = cur->pagedir;
|
||||
|
||||
@@ -110,10 +110,14 @@ syscall_exit (int status)
|
||||
}
|
||||
|
||||
static pid_t
|
||||
syscall_exec (const char *cmd_line UNUSED)
|
||||
syscall_exec (const char *cmd_line)
|
||||
{
|
||||
//TODO
|
||||
return 0;
|
||||
/* To check the end we would need to traverse the null-terminate string,
|
||||
which is equally unsafe as just leaving process_execute to do it. */
|
||||
cmd_line = validate_user_pointer (cmd_line, 1);
|
||||
if (cmd_line == NULL)
|
||||
thread_exit ();
|
||||
process_execute (cmd_line);
|
||||
}
|
||||
|
||||
static int
|
||||
|
||||
Reference in New Issue
Block a user