Skip to content

Commit

Permalink
[#13] Manage process file list
Browse files Browse the repository at this point in the history
  • Loading branch information
hangpark committed Apr 12, 2017
1 parent 292164b commit 95fbefd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
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 */

0 comments on commit 95fbefd

Please sign in to comment.