From 75bd3fbde07969666254b9c70a921455bcd1c45e Mon Sep 17 00:00:00 2001 From: EDiasAlberto Date: Fri, 8 Nov 2024 16:02:51 +0000 Subject: [PATCH] Implement syscall for close() and fix typing bug in fd_get_file w/ S. --- src/userprog/syscall.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/userprog/syscall.c b/src/userprog/syscall.c index faeac2d..57a5018 100644 --- a/src/userprog/syscall.c +++ b/src/userprog/syscall.c @@ -1,6 +1,7 @@ #include "userprog/syscall.h" #include "devices/shutdown.h" #include "devices/input.h" +#include "filesys/file.h" #include "filesys/filesys.h" #include "threads/vaddr.h" #include "threads/interrupt.h" @@ -261,9 +262,18 @@ syscall_tell (int fd UNUSED) } static void -syscall_close (int fd UNUSED) +syscall_close (int fd) { - //TODO + struct open_file *file_info = fd_get_file (fd); + if (file_info != NULL) + { + hash_delete (&thread_current ()->open_files, &file_info->elem); + + lock_acquire (&filesys_lock); + file_close (file_info->file); + lock_release (&filesys_lock); + free (file_info); + } } unsigned @@ -289,11 +299,11 @@ fd_get_file (int fd) { /* We have to set up a fake open_file in order to be able to search the hash table. See hash.h. */ - struct open_file *fake_file_info; - fake_file_info->fd = fd; + struct open_file fake_file_info; + fake_file_info.fd = fd; struct hash_elem *e - = hash_find (&thread_current ()->open_files, &fake_file_info->elem); + = hash_find (&thread_current ()->open_files, &fake_file_info.elem); return hash_entry (e, struct open_file, elem); }