Skip to content
Snippets Groups Projects
Verified Commit 197ad95e authored by ja3-saxby's avatar ja3-saxby
Browse files

Added a very basic system for associating file descriptors with file pointers

parent a71f0235
No related branches found
No related tags found
2 merge requests!22Merge Feature/system calls,!13Added a very basic system for associating file descriptors with file pointers
...@@ -68,6 +68,7 @@ userprog_SRC += userprog/syscall_halt.c ...@@ -68,6 +68,7 @@ userprog_SRC += userprog/syscall_halt.c
userprog_SRC += userprog/syscall_wait.c userprog_SRC += userprog/syscall_wait.c
userprog_SRC += userprog/syscall_create.c userprog_SRC += userprog/syscall_create.c
userprog_SRC += userprog/syscall_write.c userprog_SRC += userprog/syscall_write.c
userprog_SRC += userprog/file_descriptors_map.c
# No virtual memory code yet. # No virtual memory code yet.
#vm_SRC = vm/file.c # Some file. #vm_SRC = vm/file.c # Some file.
......
File added
No preview for this file type
/*
* File Descriptors mapper
*
* Authored by Joshua Saxby
*/
#include <stddef.h>
#include "filesys/filesys.h"
#include "system_calls.h"
// this means we can't have more than 255 open file handles at a time
#define MAX_FILE_POINTERS 255
static struct file* FILE_POINTERS_MAP[MAX_FILE_POINTERS] = {0};
struct file * get_associated_file_pointer(int fd) {
switch (fd) {
case 0: // keyboard (stdin)
case 1: // console (stdout)
case 2: // console (stderr)
return NULL;
default:
return FILE_POINTERS_MAP[fd - 3]; // exclude special fds
}
}
int associate_new_file_descriptor(struct file* file_pointer) {
// if we have been passed NULL, refuse (it's silly!)
if (file_pointer == NULL) return -1;
// find the first decriptor which points to NULL
for (size_t i = 0; i < MAX_FILE_POINTERS; i++) {
if (FILE_POINTERS_MAP[i] == NULL) {
FILE_POINTERS_MAP[i] = file_pointer;
return i + 3; // exclude special fds
}
}
// if we got here, we've run out of spare slots, return failure
return -1;
}
bool disassociate_file_descriptor(int fd) {
// clear the descriptor to NULL
switch (fd) {
case 0: // keyboard (stdin)
case 1: // console (stdout)
case 2: // console (stderr)
return false;
default:
if (FILE_POINTERS_MAP[fd - 3] != NULL) { // exclude special fds
FILE_POINTERS_MAP[fd - 3] = NULL;
return true;
} else {
return false;
}
}
}
#include "filesys/file.h"
#include "threads/interrupt.h" #include "threads/interrupt.h"
/* /*
...@@ -46,3 +47,16 @@ void syscall_write(struct intr_frame *f); ...@@ -46,3 +47,16 @@ void syscall_write(struct intr_frame *f);
* NOTE: There are more system calls implemented by Pintos but we are not * NOTE: There are more system calls implemented by Pintos but we are not
* implementing them because the assignment brief does not ask of it. * implementing them because the assignment brief does not ask of it.
*/ */
/*
* special additional stuff for handling file descriptors because they're annoying
*/
// returns NULL if the given file descriptor does not match a known file
struct file * get_associated_file_pointer(int fd);
// remembers the given file, and returns int of file descriptor
// returns -1 if could not store it (means we've opened too many files)
int associate_new_file_descriptor(struct file* file_pointer);
// disassociates the given file descriptor (and its associated pointer)
// returns false if this failed for some reason
bool disassociate_file_descriptor(int fd);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment