Update thread and syscall to use local fd counter instead global one, preventing overflow

This commit is contained in:
sBubshait
2024-11-15 15:48:56 +00:00
parent 7daf4fb079
commit 6b1dbdd34f
4 changed files with 8 additions and 6 deletions

View File

@@ -688,6 +688,7 @@ init_thread (struct thread *t, const char *name, int nice, int priority,
t->recent_cpu = recent_cpu;
t->priority = t->base_priority;
t->fd_counter = MINIMUM_USER_FD;
t->exit_status = -1;
list_init (&t->child_results);

View File

@@ -32,6 +32,9 @@ typedef int tid_t;
#define NICE_DEFAULT 0 /* Default niceness. */
#define NICE_MAX 20 /* Highest niceness. */
/* File Descriptors. */
#define MINIMUM_USER_FD 2 /* Minimum file descriptor for user programs. */
/* A process result, synchronised between parent and child. */
struct process_result
{
@@ -137,7 +140,9 @@ struct thread
#ifdef USERPROG
/* Owned by userprog/process.c. */
uint32_t *pagedir; /* Page directory. */
struct hash open_files; /* Hash Table of FD -> Struct File */
unsigned int fd_counter; /* File descriptor counter for thread's
open files. */
struct hash open_files; /* Hash Table of FD -> Struct File. */
#endif
/* Owned by thread.c. */

View File

@@ -16,8 +16,6 @@
#define MAX_SYSCALL_ARGS 3
#define EXIT_FAILURE -1
static unsigned fd_counter = MIN_USER_FD;
struct open_file
{
int fd; /* File Descriptor / Identifier */
@@ -224,7 +222,7 @@ syscall_open (const char *file)
}
/* Populate the above struct, with a unique FD and the current open file */
file_info->fd = fd_counter++;
file_info->fd = thread_current ()->fd_counter++;
file_info->file = ptr;
/* Add the new FD->file mapping to the hashtable for the current thread */

View File

@@ -4,8 +4,6 @@
#include <hash.h>
#include "threads/synch.h"
#define MIN_USER_FD 2
typedef int pid_t;
struct lock filesys_lock;