Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#13] Implement open system call #63

Merged
merged 2 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions src/userprog/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
#include "threads/vaddr.h"
#include "threads/malloc.h"

#define FD_MIN 2 /* Min value for file descriptors. */

/* Structure for arguments. */
struct arguments
{
int argc; /* Number of arguments. */
char **argv; /* Array of arguments. */
int argc; /* Number of arguments. */
char **argv; /* Array of arguments. */
};

static thread_func start_process NO_RETURN;
Expand Down Expand Up @@ -105,6 +107,7 @@ start_process (void *arguments)
else
curr->status |= PROCESS_FAIL;
curr->exec_file = exec_file;
curr->fd_next = FD_MIN;
list_init (&curr->file_list);

/* If load failed, quit. */
Expand Down Expand Up @@ -166,6 +169,13 @@ process_exit (void)
proc->status |= PROCESS_EXIT;
list_remove (&proc->elem);
file_close (proc->exec_file);
for (e = list_begin (&proc->file_list); e != list_end (&proc->file_list);)
{
struct process_file *pfe = list_entry (e, struct process_file, elem);
e = list_next (e);
file_close (pfe->file);
free (pfe);
}

struct thread *curr = thread_current ();
uint32_t *pd;
Expand Down Expand Up @@ -225,6 +235,41 @@ process_find_child (struct process *proc, pid_t pid)
}
return NULL;
}

/* Returns a process' file by the file descriptor. */
struct file *
process_get_file (int fd)
{
struct list *list = &process_current ()->file_list;
struct list_elem *e;
for (e = list_begin (list); e != list_end (list); e = list_next (e))
{
struct process_file *pfe = list_entry (e, struct process_file, elem);
if (pfe->fd == fd)
return pfe->file;
}
return NULL;
}

/* Sets the file into the current process and returns the file descriptor. */
int
process_set_file (struct file *file)
{
/* Create a file element. */
struct process_file *pfe;
pfe = (struct process_file *) malloc (sizeof (struct process_file));
if (pfe == NULL)
return -1;

/* Initialize the file element. */
struct process *curr = process_current ();
pfe->fd = curr->fd_next++;
pfe->file = file;
list_push_back (&curr->file_list, &pfe->elem);

/* Return the file descriptor. */
return pfe->fd;
}

/* We load ELF binaries. The following definitions are taken
from the ELF specification, [ELF1], more-or-less verbatim. */
Expand Down
3 changes: 3 additions & 0 deletions src/userprog/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct process
int status; /* Process status. */
int exit_code; /* Exit code. */
bool is_waiting; /* Whether parent is waiting or not. */
int fd_next; /* File descriptor tracker. */
};

/* A file held by some process. */
Expand All @@ -42,5 +43,7 @@ void process_exit (void);
void process_activate (void);
struct process *process_current (void);
struct process *process_find_child (struct process *proc, pid_t pid);
struct file *process_get_file (int fd);
int process_set_file (struct file * file);

#endif /* userprog/process.h */
21 changes: 20 additions & 1 deletion src/userprog/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,29 @@ syscall_remove (const char *file)
return success;
}

/* Opens the file. Returns a nonnegative integer handle,
a file descriptor, or -1 if the file could not be opened. */
static int
syscall_open (const char *file)
{
return 0;
/* Check the validity. */
validate_ptr (file);

/* Open the file. */
lock_acquire (&filesys_lock);
struct file *f = filesys_open (file);
if (f == NULL)
{
lock_release (&filesys_lock);
return -1;
}

/* Set the file. */
int fd = process_set_file (f);

/* Return the file descriptor. */
lock_release (&filesys_lock);
return fd;
}

static int
Expand Down