From d33fe03b2fd2ba0fd559c52a1598593238767bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sun, 21 Feb 2021 21:18:13 +0100 Subject: free parent_child when possible --- src/userprog/process.c | 41 +++++++++++++++++++++++++++++++++-------- src/userprog/syscall.c | 10 +++++++++- 2 files changed, 42 insertions(+), 9 deletions(-) (limited to 'src/userprog') diff --git a/src/userprog/process.c b/src/userprog/process.c index e99374b..39f063b 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -14,6 +14,7 @@ #include "threads/flags.h" #include "threads/init.h" #include "threads/interrupt.h" +#include "threads/malloc.h" #include "threads/palloc.h" #include "threads/synch.h" #include "threads/thread.h" @@ -42,12 +43,8 @@ tid_t process_execute (const char *file_name) { struct start_process_args args; // stack allocated since we know we wait for thread_create to finish reading - struct thread *t; tid_t tid; - t = thread_current (); - printf("%s starting %s\n", t->name, file_name); - sema_init (&args.sema, 0); /* Make a copy of FILE_NAME. @@ -68,7 +65,7 @@ process_execute (const char *file_name) sema_down (&args.sema); if (args.success) { - list_push_back (&t->children, &args.child->elem); + list_push_back (&thread_current ()->children, &args.child->elem); } else { tid = -1; } @@ -98,10 +95,15 @@ start_process (void *args_) args->success = success; if (success) { - sema_init (&t->parent.exit_sema, 0); - lock_init (&t->parent.l); + t->parent = malloc (sizeof (struct parent_child)); + + // sema_init (&t->parent.exit_sema, 0); + lock_init (&t->parent->l); - args->child = &t->parent; + t->parent->child_tid = t->tid; + t->parent->alive_count = 2; + + args->child = t->parent; } sema_up (&args->sema); @@ -135,6 +137,19 @@ process_wait (tid_t child_tid UNUSED) for (;;) {} } +static void +free_pc (struct parent_child *pc) +{ + lock_acquire (&pc->l); + + pc->alive_count--; + if (pc->alive_count == 0) { + free (pc); + } else { + lock_release (&pc->l); + } +} + /* Free the current process's resources. */ void process_exit (void) @@ -142,6 +157,16 @@ process_exit (void) struct thread *cur = thread_current (); uint32_t *pd; + free_pc (cur->parent); + + struct list_elem *e; + struct parent_child *child; + while (!list_empty (&cur->children)) { + e = list_pop_front (&cur->children); + child = list_entry (e, struct parent_child, elem); + free_pc (child); + } + /* Destroy the current process's page directory and switch back to the kernel-only page directory. */ pd = cur->pagedir; diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index 0df46f4..177cf02 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -69,7 +69,7 @@ get_fd (struct thread *thread, int fd_i) } static void -exit (int status UNUSED) +exit (int status) { struct thread *thread = thread_current (); @@ -83,6 +83,14 @@ exit (int status UNUSED) } free (thread->fds); } + + lock_acquire (&thread->parent->l); + thread->parent->exit_status = status; + lock_release (&thread->parent->l); + + printf("%s: exit(%d)\n", thread->name, status); + + thread_exit (); } static int -- cgit v1.2.1