diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-02-21 21:18:13 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-02-21 21:18:13 +0100 |
| commit | d33fe03b2fd2ba0fd559c52a1598593238767bbc (patch) | |
| tree | 5393a28e53c3683d7c77bf76efaae19a4761db4f /src/userprog/process.c | |
| parent | e40beeb936ff887afa9603c93aa804a4e5b4c7a9 (diff) | |
| download | pintos-d33fe03b2fd2ba0fd559c52a1598593238767bbc.tar.gz | |
free parent_child when possible
Diffstat (limited to 'src/userprog/process.c')
| -rw-r--r-- | src/userprog/process.c | 41 |
1 files changed, 33 insertions, 8 deletions
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; |
