diff --git a/code/userprog/addrspace.cc b/code/userprog/addrspace.cc index fee6ced..fa9784e 100644 --- a/code/userprog/addrspace.cc +++ b/code/userprog/addrspace.cc @@ -132,7 +132,7 @@ AddrSpace::AddrSpace (OpenFile * executable) pageTable[0].valid = FALSE; // Catch NULL dereference #ifdef CHANGED - int bitmapSize = UserStacksAreaSize / UserStackSize; + int bitmapSize =( UserStacksAreaSize / UserStackSize)-1; DEBUG('x', "Initialise thread counter\n"); threads = 1; @@ -325,20 +325,26 @@ AddrSpace::AllocateUserStack() /* If we don't have any free slot, we can wait for one to be * freed by DeAllocateUserstack */ - while ((bit = memoryMap->Find()) == - 1){ - DEBUG('x', "Can't allocate User Stack Wainting...\n"); - semAllocateUserStack->P(); + + semAllocateUserStack->P(); + bit = memoryMap->Find(); + semAllocateUserStack->V(); + if ( bit == - 1 ) { + DEBUG('x', "No slot avaible on User Stack\n"); + return -1; } int addr = memory - UserStackSize * bit; DEBUG('x', "Allocate User Stack bit %d, addr:%x\n", bit, addr); return addr; } + void AddrSpace::DeAllocateUserStack(int addr){ - int memory = numPages * PageSize; + int memory = numPages * PageSize; int bit = (memory - addr) / UserStackSize; DEBUG('x', "Deallocate User Stack bit %d, addr:%x\n", bit, addr); + semAllocateUserStack->P(); memoryMap->Clear(bit); - semAllocateUserStack->V(); + semAllocateUserStack->P(); } #endif diff --git a/code/userprog/userthread.cc b/code/userprog/userthread.cc index f1b0aa2..f2eb94e 100644 --- a/code/userprog/userthread.cc +++ b/code/userprog/userthread.cc @@ -15,7 +15,6 @@ static void StartUserThread( void * args ){ // because we only have a void in our function definition ThreadArgs_t * cpy_args; cpy_args = (ThreadArgs_t *) args; - int stack_addr = currentThread->space->AllocateUserStack(); // init register for (int i = 0; i < NumTotalRegs; i++ ) { @@ -32,8 +31,8 @@ static void StartUserThread( void * args ){ DEBUG('x',"Register 4: 0x%x\n", cpy_args->arg); // init stack - machine->WriteRegister(StackReg, stack_addr); - DEBUG('x',"StackRegister: 0x%x\n", stack_addr); + machine->WriteRegister(StackReg, cpy_args->stackAddr); + DEBUG('x',"StackRegister: 0x%x\n", cpy_args->stackAddr); // All our registers have values, we can desallocate our cpy_args free(cpy_args); @@ -51,7 +50,13 @@ int do_ThreadCreate(int f, int arg){ ThreadArgs_t * args = (ThreadArgs_t *) malloc(sizeof(ThreadArgs_t)); args->f = f; args->arg = arg; - + + // Check if we can allocate Stack for our son + int stackAddr = currentThread->space->AllocateUserStack(); + if ( stackAddr == -1 ) { + fprintf(stderr, "Segmentation Fault - no space avaible on stack\n"); + Exit(1); + } // create a new Thread and start it Thread * newThread = new Thread("new thread"); diff --git a/code/userprog/userthread.h b/code/userprog/userthread.h index 7510d4c..e49b51b 100644 --- a/code/userprog/userthread.h +++ b/code/userprog/userthread.h @@ -6,6 +6,7 @@ typedef struct { int f; int arg; + int stackAddr; } ThreadArgs_t; extern int do_ThreadCreate(int f, int arg);