Implement syscall for file opening and refactor open_files initialisation in thread.c w/ S.
This commit is contained in:
@@ -238,6 +238,7 @@ thread_create (const char *name, int priority,
|
|||||||
struct thread *parent_thread = thread_current ();
|
struct thread *parent_thread = thread_current ();
|
||||||
init_thread (t, name, parent_thread->nice, priority, parent_thread->recent_cpu);
|
init_thread (t, name, parent_thread->nice, priority, parent_thread->recent_cpu);
|
||||||
tid = t->tid = allocate_tid ();
|
tid = t->tid = allocate_tid ();
|
||||||
|
hash_init (&t->open_files, fd_hash, fd_less, NULL);
|
||||||
|
|
||||||
/* Prepare thread for first run by initializing its stack.
|
/* Prepare thread for first run by initializing its stack.
|
||||||
Do this atomically so intermediate values for the '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->priority = t->base_priority;
|
||||||
|
|
||||||
t->exit_status = -1;
|
t->exit_status = -1;
|
||||||
hash_init (&t->open_files, fd_hash, fd_less, NULL);
|
|
||||||
|
|
||||||
old_level = intr_disable ();
|
old_level = intr_disable ();
|
||||||
list_push_back (&all_list, &t->allelem);
|
list_push_back (&all_list, &t->allelem);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "filesys/filesys.h"
|
#include "filesys/filesys.h"
|
||||||
#include "threads/vaddr.h"
|
#include "threads/vaddr.h"
|
||||||
#include "threads/interrupt.h"
|
#include "threads/interrupt.h"
|
||||||
|
#include "threads/malloc.h"
|
||||||
#include "threads/thread.h"
|
#include "threads/thread.h"
|
||||||
#include "threads/synch.h"
|
#include "threads/synch.h"
|
||||||
#include "userprog/process.h"
|
#include "userprog/process.h"
|
||||||
@@ -12,11 +13,12 @@
|
|||||||
#include <syscall-nr.h>
|
#include <syscall-nr.h>
|
||||||
|
|
||||||
static struct lock filesys_lock;
|
static struct lock filesys_lock;
|
||||||
|
static unsigned fd_counter = MIN_USER_FD;
|
||||||
|
|
||||||
struct open_file
|
struct open_file
|
||||||
{
|
{
|
||||||
int fd; /* File Descriptor / Identifier */
|
int fd; /* File Descriptor / Identifier */
|
||||||
struct file *file; /* Pointer to the associated file */
|
struct file *file; /* Pointer to the associated file */
|
||||||
struct hash_elem elem; /* elem for a hash table */
|
struct hash_elem elem; /* elem for a hash table */
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -165,10 +167,27 @@ syscall_remove (const char *file)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
syscall_open (const char *file UNUSED)
|
syscall_open (const char *file)
|
||||||
{
|
{
|
||||||
//TODO
|
validate_user_pointer (file, 1);
|
||||||
return 0;
|
|
||||||
|
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
|
static int
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <hash.h>
|
#include <hash.h>
|
||||||
|
|
||||||
|
#define MIN_USER_FD 2
|
||||||
|
|
||||||
typedef int pid_t;
|
typedef int pid_t;
|
||||||
|
|
||||||
void syscall_init (void);
|
void syscall_init (void);
|
||||||
|
|||||||
Reference in New Issue
Block a user