|
|
|
@@ -47,6 +47,9 @@ struct process_start_data
|
|
|
|
tokens while maintaining state. */
|
|
|
|
tokens while maintaining state. */
|
|
|
|
char file_name[FNAME_MAX_LEN + 1]; /* Name of the file of the process to
|
|
|
|
char file_name[FNAME_MAX_LEN + 1]; /* Name of the file of the process to
|
|
|
|
be started. */
|
|
|
|
be started. */
|
|
|
|
|
|
|
|
bool success; /* Indicates whether the process was successfully loaded. */
|
|
|
|
|
|
|
|
struct semaphore loaded; /* Semaphore used to signal that the process has
|
|
|
|
|
|
|
|
been loaded. */
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static thread_func start_process NO_RETURN;
|
|
|
|
static thread_func start_process NO_RETURN;
|
|
|
|
@@ -67,6 +70,8 @@ process_execute (const char *cmd)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return TID_ERROR;
|
|
|
|
return TID_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sema_init (&data->loaded, 0);
|
|
|
|
|
|
|
|
data->success = false;
|
|
|
|
|
|
|
|
|
|
|
|
/* Make a copy of command.
|
|
|
|
/* Make a copy of command.
|
|
|
|
Otherwise there's a race between the caller and load(). */
|
|
|
|
Otherwise there's a race between the caller and load(). */
|
|
|
|
@@ -100,7 +105,14 @@ process_execute (const char *cmd)
|
|
|
|
|
|
|
|
|
|
|
|
tid = thread_create (file_name, PRI_DEFAULT, start_process, data);
|
|
|
|
tid = thread_create (file_name, PRI_DEFAULT, start_process, data);
|
|
|
|
if (tid == TID_ERROR)
|
|
|
|
if (tid == TID_ERROR)
|
|
|
|
palloc_free_page (cmd_copy);
|
|
|
|
palloc_free_page (cmd_copy);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sema_down (&data->loaded);
|
|
|
|
|
|
|
|
if (!data->success)
|
|
|
|
|
|
|
|
tid = TID_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
free (data);
|
|
|
|
return tid;
|
|
|
|
return tid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -132,6 +144,11 @@ start_process (void *proc_start_data)
|
|
|
|
|
|
|
|
|
|
|
|
/* Prevent writing to the file being executed. */
|
|
|
|
/* Prevent writing to the file being executed. */
|
|
|
|
struct file *exec_file = filesys_open (data->file_name);
|
|
|
|
struct file *exec_file = filesys_open (data->file_name);
|
|
|
|
|
|
|
|
if (exec_file == NULL)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
lock_release (&filesys_lock);
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
}
|
|
|
|
thread_current ()->exec_file = exec_file;
|
|
|
|
thread_current ()->exec_file = exec_file;
|
|
|
|
file_deny_write (exec_file);
|
|
|
|
file_deny_write (exec_file);
|
|
|
|
lock_release (&filesys_lock);
|
|
|
|
lock_release (&filesys_lock);
|
|
|
|
@@ -153,10 +170,11 @@ start_process (void *proc_start_data)
|
|
|
|
/* If stack initialization failed, free resources and quit. */
|
|
|
|
/* If stack initialization failed, free resources and quit. */
|
|
|
|
if (!success)
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
process_exit ();
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data->success = true;
|
|
|
|
|
|
|
|
sema_up (&data->loaded);
|
|
|
|
/* Start the user process by simulating a return from an
|
|
|
|
/* Start the user process by simulating a return from an
|
|
|
|
interrupt, implemented by intr_exit (in
|
|
|
|
interrupt, implemented by intr_exit (in
|
|
|
|
threads/intr-stubs.S). Because intr_exit takes all of its
|
|
|
|
threads/intr-stubs.S). Because intr_exit takes all of its
|
|
|
|
@@ -166,9 +184,10 @@ start_process (void *proc_start_data)
|
|
|
|
asm volatile ("movl %0, %%esp; jmp intr_exit" : : "g" (&if_) : "memory");
|
|
|
|
asm volatile ("movl %0, %%esp; jmp intr_exit" : : "g" (&if_) : "memory");
|
|
|
|
NOT_REACHED ();
|
|
|
|
NOT_REACHED ();
|
|
|
|
|
|
|
|
|
|
|
|
/* If starting the process failed, free its common resources and exit. */
|
|
|
|
/* If starting the process failed, exit. */
|
|
|
|
fail:
|
|
|
|
fail:
|
|
|
|
free (data);
|
|
|
|
data->success = false;
|
|
|
|
|
|
|
|
sema_up (&data->loaded);
|
|
|
|
thread_exit ();
|
|
|
|
thread_exit ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -333,7 +352,6 @@ process_exit (void)
|
|
|
|
if (cur->exec_file != NULL)
|
|
|
|
if (cur->exec_file != NULL)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lock_acquire (&filesys_lock);
|
|
|
|
lock_acquire (&filesys_lock);
|
|
|
|
file_allow_write (cur->exec_file);
|
|
|
|
|
|
|
|
file_close (cur->exec_file);
|
|
|
|
file_close (cur->exec_file);
|
|
|
|
lock_release (&filesys_lock);
|
|
|
|
lock_release (&filesys_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -361,10 +379,11 @@ process_exit (void)
|
|
|
|
/* Free child process results or signal parent's death. */
|
|
|
|
/* Free child process results or signal parent's death. */
|
|
|
|
struct list_elem *e;
|
|
|
|
struct list_elem *e;
|
|
|
|
for (e = list_begin (&cur->child_results);
|
|
|
|
for (e = list_begin (&cur->child_results);
|
|
|
|
e != list_end (&cur->child_results); e = list_next (e))
|
|
|
|
e != list_end (&cur->child_results);)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
struct process_result *result
|
|
|
|
struct process_result *result
|
|
|
|
= list_entry (e, struct process_result, elem);
|
|
|
|
= list_entry (e, struct process_result, elem);
|
|
|
|
|
|
|
|
struct list_elem *next = list_next (e);
|
|
|
|
lock_acquire (&result->lock);
|
|
|
|
lock_acquire (&result->lock);
|
|
|
|
/* Child has died (and was not waited for). Free the result. */
|
|
|
|
/* Child has died (and was not waited for). Free the result. */
|
|
|
|
if (sema_try_down (&result->sema))
|
|
|
|
if (sema_try_down (&result->sema))
|
|
|
|
@@ -378,6 +397,7 @@ process_exit (void)
|
|
|
|
sema_up (&result->sema);
|
|
|
|
sema_up (&result->sema);
|
|
|
|
lock_release (&result->lock);
|
|
|
|
lock_release (&result->lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Destroy the current process's page directory and switch back
|
|
|
|
/* Destroy the current process's page directory and switch back
|
|
|
|
|