Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
syscall open
Browse files Browse the repository at this point in the history
  • Loading branch information
CCXXXI committed Jan 10, 2021
1 parent 850e3fa commit a4f9da3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/userprog/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ struct process *process_create(struct thread *t)
sema_init(&p->sema_load, 0);
sema_init(&p->sema_wait, 0);

list_init(&p->files);
p->fd = 2;

return p;
}

Expand Down
2 changes: 2 additions & 0 deletions src/userprog/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ struct process
struct process *parent; /* Parent. */
struct semaphore sema_load; /* Parent block on this while loading. */
struct semaphore sema_wait; /* Parent block on this while waiting. */
struct list files; /* Opening files. */
int fd; /* Max file descriptor num. */
};

void process_init(void);
Expand Down
27 changes: 25 additions & 2 deletions src/userprog/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "threads/vaddr.h"
#include "threads/synch.h"
#include "threads/palloc.h"
#include "threads/malloc.h"
#include "userprog/pagedir.h"
#include "userprog/process.h"
#include "devices/shutdown.h"
Expand All @@ -21,6 +22,13 @@
exit(-1); \
}

static struct open_file
{
int fd;
struct file *file;
struct list_elem elem;
};

static void syscall_handler(struct intr_frame *);
static bool is_valid_ptr(const void *ptr);
static bool is_user_mem(const void *start, size_t size);
Expand Down Expand Up @@ -350,8 +358,23 @@ static bool remove(const char *file)
file position. */
static int open(const char *file)
{
// todo
return -1;
USER_ASSERT(is_valid_str(file));

lock_acquire(&file_lock);
struct file *f = filesys_open(file);
lock_release(&file_lock);

if (f == NULL)
return -1;

struct process *self = thread_current()->process;

struct open_file *open_file = malloc(sizeof(struct open_file));
open_file->fd = self->fd++;
open_file->file = f;
list_push_back(&self->files, &open_file->elem);

return open_file->fd;
}

/* Returns the size, in bytes, of the file open as FD. */
Expand Down

0 comments on commit a4f9da3

Please sign in to comment.