Combine syscall code with final stack initialization code #32
@@ -10,6 +10,7 @@
|
|||||||
#include "threads/flags.h"
|
#include "threads/flags.h"
|
||||||
#include "threads/interrupt.h"
|
#include "threads/interrupt.h"
|
||||||
#include "threads/intr-stubs.h"
|
#include "threads/intr-stubs.h"
|
||||||
|
#include "threads/malloc.h"
|
||||||
#include "threads/palloc.h"
|
#include "threads/palloc.h"
|
||||||
#include "threads/switch.h"
|
#include "threads/switch.h"
|
||||||
#include "threads/synch.h"
|
#include "threads/synch.h"
|
||||||
@@ -70,6 +71,7 @@ static void kernel_thread (thread_func *, void *aux);
|
|||||||
static void idle (void *aux UNUSED);
|
static void idle (void *aux UNUSED);
|
||||||
static struct thread *running_thread (void);
|
static struct thread *running_thread (void);
|
||||||
static struct thread *next_thread_to_run (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,
|
static void init_thread (struct thread *, const char *name, int nice,
|
||||||
int priority, fp32_t recent_cpu);
|
int priority, fp32_t recent_cpu);
|
||||||
static bool is_thread (struct thread *) UNUSED;
|
static bool is_thread (struct thread *) UNUSED;
|
||||||
@@ -112,6 +114,7 @@ thread_init (void)
|
|||||||
initial_thread_recent_cpu);
|
initial_thread_recent_cpu);
|
||||||
initial_thread->status = THREAD_RUNNING;
|
initial_thread->status = THREAD_RUNNING;
|
||||||
initial_thread->tid = allocate_tid ();
|
initial_thread->tid = allocate_tid ();
|
||||||
|
initial_thread->result = NULL; /* Main thread cannot be waited for. */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Starts preemptive thread scheduling by enabling interrupts.
|
/* Starts preemptive thread scheduling by enabling interrupts.
|
||||||
@@ -238,6 +241,7 @@ thread_create (const char *name, int priority,
|
|||||||
struct thread *parent_thread = thread_current ();
|
struct thread *parent_thread = thread_current ();
|
||||||
init_thread (t, name, parent_thread->nice, priority, parent_thread->recent_cpu);
|
init_thread (t, name, parent_thread->nice, priority, parent_thread->recent_cpu);
|
||||||
tid = t->tid = allocate_tid ();
|
tid = t->tid = allocate_tid ();
|
||||||
|
init_process_result (t);
|
||||||
|
|
||||||
#ifdef USERPROG
|
#ifdef USERPROG
|
||||||
hash_init (&t->open_files, fd_hash, fd_less, NULL);
|
hash_init (&t->open_files, fd_hash, fd_less, NULL);
|
||||||
@@ -265,6 +269,10 @@ thread_create (const char *name, int priority,
|
|||||||
|
|
||||||
intr_set_level (old_level);
|
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. */
|
/* Add to run queue. */
|
||||||
thread_unblock (t);
|
thread_unblock (t);
|
||||||
|
|
||||||
@@ -638,6 +646,18 @@ is_thread (struct thread *t)
|
|||||||
return t != NULL && t->magic == THREAD_MAGIC;
|
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
|
/* Does basic initialization of T as a blocked thread named
|
||||||
NAME. */
|
NAME. */
|
||||||
static void
|
static void
|
||||||
@@ -666,6 +686,7 @@ init_thread (struct thread *t, const char *name, int nice, int priority,
|
|||||||
t->priority = t->base_priority;
|
t->priority = t->base_priority;
|
||||||
|
|
||||||
t->exit_status = -1;
|
t->exit_status = -1;
|
||||||
|
list_init (&t->child_results);
|
||||||
|
|
||||||
old_level = intr_disable ();
|
old_level = intr_disable ();
|
||||||
list_push_back (&all_list, &t->allelem);
|
list_push_back (&all_list, &t->allelem);
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
#include <hash.h>
|
#include <hash.h>
|
||||||
#include <list.h>
|
#include <list.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "threads/synch.h"
|
||||||
#include "threads/fixed-point.h"
|
#include "threads/fixed-point.h"
|
||||||
|
|
||||||
/* States in a thread's life cycle. */
|
/* States in a thread's life cycle. */
|
||||||
@@ -30,6 +32,18 @@ typedef int tid_t;
|
|||||||
#define NICE_DEFAULT 0 /* Default niceness. */
|
#define NICE_DEFAULT 0 /* Default niceness. */
|
||||||
#define NICE_MAX 20 /* Highest 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.
|
/* A kernel thread or user process.
|
||||||
|
|
||||||
Each thread structure is stored in its own 4 kB page. The
|
Each thread structure is stored in its own 4 kB page. The
|
||||||
@@ -109,6 +123,11 @@ struct thread
|
|||||||
int nice; /* Nice value for this thread */
|
int nice; /* Nice value for this thread */
|
||||||
fp32_t recent_cpu; /* Amount of time this process received */
|
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. */
|
/* Shared between thread.c and synch.c. */
|
||||||
struct list_elem elem; /* List element. */
|
struct list_elem elem; /* List element. */
|
||||||
|
|
||||||
|
|||||||
@@ -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,39 @@ 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);
|
||||||
|
/* 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. */
|
/* Free the current process's resources. */
|
||||||
@@ -219,6 +247,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
|
||||||
|
{
|
||||||
|
lock_release (&cur->result->lock);
|
||||||
|
sema_up (&cur->result->sema);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
lock_release (&result->lock);
|
||||||
|
sema_up (&result->sema);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* 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