Merge branch 'read-only-exec' into 'userprog-merge'

Combine syscall code with final stack initialization code

See merge request lab2425_autumn/pintos_22!32
This commit is contained in:
Demetriades, Themis
2024-11-11 22:23:20 +00:00
5 changed files with 345 additions and 32 deletions

View File

@@ -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>
@@ -136,6 +137,10 @@ start_process (void *proc_start_data)
goto fail;
}
struct file *exec_file = filesys_open (file_name);
thread_current ()->exec_file = exec_file;
file_deny_write (exec_file);
/* Start the user process by simulating a return from an
interrupt, implemented by intr_exit (in
threads/intr-stubs.S). Because intr_exit takes all of its
@@ -261,12 +266,39 @@ push_to_stack (void **esp, void *data, size_t data_size)
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. */
@@ -277,6 +309,48 @@ process_exit (void)
uint32_t *pd;
printf ("%s: exit(%d)\n", cur->name, cur->exit_status);
file_close (cur->exec_file);
/* 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
to the kernel-only page directory. */