hw5: mostly done

This commit is contained in:
Claudio Maggioni 2020-05-25 19:37:51 +02:00
parent aa03d859f6
commit de7abb4f0d
5 changed files with 47 additions and 37 deletions

View file

@ -1,5 +1,9 @@
{
"files.associations": {
"ratio": "c"
"ratio": "c",
"__functional_03": "c",
"functional": "c",
"locale": "c",
"*.inc": "c"
}
}

View file

@ -440,7 +440,7 @@ thread_exit (void)
printf("%s: exit(%d)\n", t->name, t->exit_status);
list_remove (&t->allelem);
sema_up(&t->waiting_sem);
sema_down(&t->parent->parent_sem);
sema_down(&t->parent_sem);
t->status = THREAD_DYING;
list_remove(&t->child_elem);
schedule ();

View file

@ -18,6 +18,8 @@
#include "threads/thread.h"
#include "threads/vaddr.h"
#include "lib/kernel/list.h"
#include "userprog/syscall.h"
#include "threads/synch.h"
static struct arg {
struct list_elem elem;
@ -59,11 +61,13 @@ process_execute (const char *file_name)
}
strlcpy(program_name, file_name, i+1);
lock_acquire(&filesys_lock);
struct file* f = filesys_open (program_name);
if (f == NULL) {
return TID_ERROR;
}
file_close(f);
lock_release(&filesys_lock);
/* Create a new thread to execute FILE_NAME. */
tid = thread_create (program_name, PRI_DEFAULT, start_process, fn_copy);
@ -104,6 +108,7 @@ start_process (void *file_name_)
if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
if_.cs = SEL_UCSEG;
if_.eflags = FLAG_IF | FLAG_MBS;
//printf("Loading %s\n", program_name);
success = load (program_name, &if_.eip, &if_.esp);
list_reverse(&arg_list);
@ -200,7 +205,7 @@ process_wait (tid_t child_tid)
int status = child->exit_status;
//printf("Wait exit: %s %d\n",child->name, status);
child->waited_on_before = 1;
sema_up(&(thread_current()->parent_sem));
sema_up(&(child->parent_sem));
return status;
}
@ -334,6 +339,7 @@ load (const char *file_name, void (**eip) (void), void **esp)
goto done;
process_activate ();
lock_acquire(&filesys_lock);
/* Open executable file. */
file = filesys_open (file_name);
if (file == NULL)
@ -426,6 +432,7 @@ load (const char *file_name, void (**eip) (void), void **esp)
done:
/* We arrive here whether the load is successful or not. */
file_close (file);
lock_release(&filesys_lock);
return success;
}

View file

@ -40,7 +40,7 @@ struct fd_item {
int next_fd = 3;
struct hash fd_table;
struct semaphore filesys_lock;
struct lock filesys_lock;
static unsigned item_hash (const struct hash_elem* e, void* aux) {
struct fd_item* i = hash_entry(e, struct fd_item, elem);
@ -72,7 +72,7 @@ syscall_init (void)
syscall_vec[SYS_READ] = (handler)sys_read;
syscall_vec[SYS_WRITE] = (handler)sys_write;
hash_init(&fd_table, item_hash, item_compare, NULL);
sema_init(&filesys_lock, 1);
lock_init(&filesys_lock);
}
static bool check_ptr(const void* ptr) {
@ -90,17 +90,17 @@ void sys_halt(void) {
bool sys_create(const char* filename, unsigned initial_size) {
if (check_ptr(filename)) return 0;
sema_down (&filesys_lock);
lock_acquire (&filesys_lock);
bool return_code = filesys_create(filename, initial_size);
sema_up (&filesys_lock);
lock_release (&filesys_lock);
return return_code;
}
bool sys_remove(const char* filename) {
if (check_ptr(filename)) return 0;
sema_down (&filesys_lock);
lock_acquire (&filesys_lock);
bool return_code = filesys_remove(filename);
sema_up (&filesys_lock);
lock_release (&filesys_lock);
return return_code;
}
@ -112,40 +112,42 @@ int sys_open(const char* file) {
return -1;
}
sema_down (&filesys_lock);
lock_acquire (&filesys_lock);
file_opened = filesys_open(file);
//printf("File opened: %x\n", file_opened);
if (!file_opened) {
free(fd);
sema_up (&filesys_lock);
lock_release (&filesys_lock);
return -1;
}
fd->file = file_opened; //file save
fd->file = file_opened;
fd->fd = next_fd++;
//printf("Hash put: %x %d\n", file_opened, fd->fd);
hash_insert(&fd_table, &fd->elem);
sema_up (&filesys_lock);
lock_release (&filesys_lock);
return fd->fd;
}
int sys_filesize(int fd) {
sema_down (&filesys_lock);
lock_acquire (&filesys_lock);
struct fd_item i;
i.fd = fd;
struct fd_item* file_d = hash_entry(hash_find(&fd_table, &i.elem), struct fd_item, elem);
if(file_d == NULL) {
sema_up (&filesys_lock);
lock_release (&filesys_lock);
return -1;
}
int ret = file_length(file_d->file);
sema_up (&filesys_lock);
lock_release (&filesys_lock);
return ret;
}
void sys_seek(int fd, unsigned position) {
sema_down (&filesys_lock);
lock_acquire (&filesys_lock);
struct fd_item i;
i.fd = fd;
struct fd_item* file_d = hash_entry(hash_find(&fd_table, &i.elem), struct fd_item, elem);
@ -156,11 +158,11 @@ void sys_seek(int fd, unsigned position) {
else
return;
sema_up (&filesys_lock);
lock_release (&filesys_lock);
}
unsigned sys_tell(int fd) {
sema_down (&filesys_lock);
lock_acquire (&filesys_lock);
struct fd_item i;
i.fd = fd;
struct fd_item* file_d = hash_entry(hash_find(&fd_table, &i.elem), struct fd_item, elem);
@ -172,29 +174,30 @@ unsigned sys_tell(int fd) {
else
ret = -1;
sema_up (&filesys_lock);
lock_release (&filesys_lock);
return ret;
}
void sys_close(int fd) {
sema_down (&filesys_lock);
lock_acquire (&filesys_lock);
struct fd_item i;
i.fd = fd;
struct hash_elem* h = hash_find(&fd_table, &i.elem);
if (h == NULL) sys_exit(-1);
struct fd_item* file_d = hash_entry(h, struct fd_item, elem);
//printf("File close id: %x\n", file_d);
if(file_d && file_d->file) {
//printf("File closed: %x\n", file_d->file);
file_close(file_d->file);
hash_delete(&fd_table, &(file_d->elem));
free(file_d);
}
sema_up (&filesys_lock);
lock_release (&filesys_lock);
}
int sys_read(int fd, void *buffer, unsigned size) {
if (check_ptr(buffer)||check_ptr(buffer+size)) return -1;
sema_down (&filesys_lock);
lock_acquire (&filesys_lock);
int ret;
if(fd == 0) {
@ -206,14 +209,12 @@ int sys_read(int fd, void *buffer, unsigned size) {
if (h == NULL) sys_exit(-1);
struct fd_item* file_d = hash_entry(h, struct fd_item, elem);
if(file_d && file_d->file) {
if (file_d && file_d->file) {
ret = file_read(file_d->file, buffer, size);
}
else // no such file or can't open
ret = -1;
} else ret = -1;
}
sema_up (&filesys_lock);
lock_release (&filesys_lock);
return ret;
}
@ -224,11 +225,11 @@ int sys_write(int fd, const void *buffer, unsigned size) {
}
int ret;
if(fd == 1) { // write to stdout
if(fd == 1) {
putbuf(buffer, size);
ret = size;
} else {
sema_down (&filesys_lock);
lock_acquire (&filesys_lock);
struct fd_item i;
i.fd = fd;
struct hash_elem* h = hash_find(&fd_table, &i.elem);
@ -238,9 +239,8 @@ int sys_write(int fd, const void *buffer, unsigned size) {
if(file_d && file_d->file) {
ret = file_write(file_d->file, buffer, size);
}
else // no such file or can't open
ret = -1;
sema_up (&filesys_lock);
else ret = -1;
lock_release (&filesys_lock);
}
return ret;
@ -277,9 +277,7 @@ sys_exit (int status)
int
sys_wait (tid_t tid)
{
//printf("system call wait\n");
int status = process_wait(tid);
//printf("wait has returned status(%d)\n", status);
return status;
}
@ -289,6 +287,5 @@ sys_exec (const char* file_name) {
pagedir_get_page(thread_current()->pagedir, file_name) == NULL) {
return -1;
}
return process_execute(file_name);
}

View file

@ -3,4 +3,6 @@
void syscall_init (void);
struct lock filesys_lock;
#endif /* userprog/syscall.h */