diff --git a/src/threads/thread.c b/src/threads/thread.c index 04380dd..037f309 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -238,6 +238,7 @@ thread_create (const char *name, int priority, struct thread *parent_thread = thread_current (); init_thread (t, name, parent_thread->nice, priority, parent_thread->recent_cpu); tid = t->tid = allocate_tid (); + hash_init (&t->open_files, fd_hash, fd_less, NULL); /* Prepare thread for first run by initializing its stack. Do this atomically so intermediate values for the 'stack' @@ -662,7 +663,6 @@ init_thread (struct thread *t, const char *name, int nice, int priority, t->priority = t->base_priority; t->exit_status = -1; - hash_init (&t->open_files, fd_hash, fd_less, NULL); old_level = intr_disable (); list_push_back (&all_list, &t->allelem); diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index 555b940..f89e2f9 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -4,6 +4,7 @@ #include "filesys/filesys.h" #include "threads/vaddr.h" #include "threads/interrupt.h" +#include "threads/malloc.h" #include "threads/thread.h" #include "threads/synch.h" #include "userprog/process.h" @@ -12,11 +13,12 @@ #include static struct lock filesys_lock; +static unsigned fd_counter = MIN_USER_FD; struct open_file { - int fd; /* File Descriptor / Identifier */ - struct file *file; /* Pointer to the associated file */ + int fd; /* File Descriptor / Identifier */ + struct file *file; /* Pointer to the associated file */ struct hash_elem elem; /* elem for a hash table */ }; @@ -165,10 +167,27 @@ syscall_remove (const char *file) } static int -syscall_open (const char *file UNUSED) +syscall_open (const char *file) { - //TODO - return 0; + validate_user_pointer (file, 1); + + lock_acquire (&filesys_lock); + struct file *ptr = filesys_open (file); + lock_release (&filesys_lock); + if (ptr == NULL) + return -1; + + struct open_file *file_info + = (struct open_file*) malloc (sizeof (struct open_file)); + if (file_info == NULL) + return -1; + + file_info->fd = fd_counter++; + file_info->file = ptr; + + hash_insert (&thread_current ()->open_files, &file_info->elem); + + return file_info->fd; } static int diff --git a/src/userprog/syscall.h b/src/userprog/syscall.h index 9563059..0f9288b 100644 --- a/src/userprog/syscall.h +++ b/src/userprog/syscall.h @@ -3,6 +3,8 @@ #include +#define MIN_USER_FD 2 + typedef int pid_t; void syscall_init (void);