diff --git a/Sim04/config0.cnf b/Sim04/config0.cnf index 4654fe8..9404729 100644 --- a/Sim04/config0.cnf +++ b/Sim04/config0.cnf @@ -1,7 +1,7 @@ Start Simulator Configuration File: Version/Phase : 1.05 File Path : metadata5.mdf -CPU Scheduling Code : FCFS-P +CPU Scheduling Code : SJF-N Quantum Time (cycles) : 3 Memory Available (KB) : 11100 Memory Display (On/Off) : On diff --git a/Sim04/logfile_1.lgf b/Sim04/logfile_1.lgf index fea283a..c83aaef 100644 --- a/Sim04/logfile_1.lgf +++ b/Sim04/logfile_1.lgf @@ -1 +1 @@ -0.000001, OS: Simulator start +0.000000, OS: Simulator start diff --git a/Sim04/sim04 b/Sim04/sim04 index 30f062f..3334c45 100755 Binary files a/Sim04/sim04 and b/Sim04/sim04 differ diff --git a/Sim04/simulator.c b/Sim04/simulator.c index 3fd445b..ba49b0b 100644 --- a/Sim04/simulator.c +++ b/Sim04/simulator.c @@ -89,8 +89,6 @@ Dependencies: ??? */ bool allocMemory(ProcessControlBlock *singlePCB, int offset, int size, int arraySize) { - // printf("Memory Offset: %d\n", offset); - // printf("Memory Size: %d\n", size); int allocateIterator = 0; // while until a '-1' is hit, then store the value. while(singlePCB->allocatedMemSizes[allocateIterator] != -1 && allocateIterator < arraySize) @@ -399,13 +397,6 @@ ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *me inOutArg = wkgMetaPtr->inOutArg; } - if (configPtr->cpuSchedCode == CPU_SCHED_RR_P_CODE) - { - // if in round robin preemptive scheduling, then make the linked list - // into a circularly linked list, with the head pointer pointed to the - // first node, and the last node linked to the first. - wkgPCBPtr->nextPtr = headPointer->headPtr; - } // return headPointer return headPointer; } @@ -464,6 +455,7 @@ bool interruptManager(ConfigDataType *configPtr, ProcessControlBlock *singlePCB, // /* code */ // break; case CLEAR_INTERRUPT: + // COMMENT HERE return false; break; } @@ -486,7 +478,7 @@ Dependencies: fprintf, ??? logFileLL *logOutput(ConfigDataType *configPtr, char *logStr, bool isEnd, logFileLL *logPtr) { logFile toLog; - // logFile *logStrPtr; + logFile *logStrPtr; logFile *wkgLogPtr; logFile *prevWkgLogPtr; wkgLogPtr = logPtr->headPtr; @@ -529,8 +521,8 @@ logFileLL *logOutput(ConfigDataType *configPtr, char *logStr, bool isEnd, logFil } if (wkgLogPtr == NULL) { - // logStrPtr = addLog(toLog); - // prevWkgLogPtr->nextPtr = addLog(toLog); // THIS SHOULD BE THE WORKING LINE, but it doesn't. + logStrPtr = addLog(toLog); + // prevWkgLogPtr->nextPtr = addLog(toLog); // prevWkgLogPtr = addLog(toLog); // prevWkgLogPtr->nextPtr = logStrPtr; // prevWkgLogPtr->nextPtr = logStrPtr; @@ -560,192 +552,190 @@ Device Input/Device: none Device Output/Device: none Dependencies: ??? */ -// bool memManager(ProcessControlBlockLL *pcbList, MEMORY_SIGNALS memCode, int offset, int size, ConfigDataType *configPtr, -// ProcessControlBlock *singlePCB) -// { -// // printf("Memory Offset: %d\n", offset); -// // printf("Memory Size: %d\n", size); -// bool memDisplay = configPtr->memDisplay; -// ProcessControlBlock *wkgPtr; -// int availableMem = configPtr->memAvailable; -// int usedMem = 0; -// int currentOffset = 0; -// int currentSize = 0; -// int memIterator; -// int *memorySizeArray; -// int totalArraySize = 0; -// bool subFunctionSuccess = true; -// wkgPtr = pcbList->headPtr; +bool memManager(ProcessControlBlockLL *pcbList, MEMORY_SIGNALS memCode, int offset, int size, ConfigDataType *configPtr, + ProcessControlBlock *singlePCB) +{ + bool memDisplay = configPtr->memDisplay; + ProcessControlBlock *wkgPtr; + int availableMem = configPtr->memAvailable; + int usedMem = 0; + int currentOffset = 0; + int currentSize = 0; + int memIterator; + int *memorySizeArray; + int totalArraySize = 0; + bool subFunctionSuccess = true; + wkgPtr = pcbList->headPtr; -// // grab how much memory is being used total, then count how big the total memory array should be -// while(wkgPtr != NULL) -// { -// // count up the total used memory within each process control block. -// for(memIterator = 0; memIterator < wkgPtr->memArraySize; memIterator++) -// { -// if(wkgPtr->allocatedMemSizes[memIterator] != -1) -// { -// // grab how much memory is used by that process -// usedMem = usedMem + wkgPtr->allocatedMemSizes[memIterator]; -// } -// totalArraySize = totalArraySize + 1; -// } -// // move to the next pointer -// wkgPtr = wkgPtr->nextPtr; -// } + // grab how much memory is being used total, then count how big the total memory array should be + while(wkgPtr != NULL) + { + // count up the total used memory within each process control block. + for(memIterator = 0; memIterator < wkgPtr->memArraySize; memIterator++) + { + if(wkgPtr->allocatedMemSizes[memIterator] != -1) + { + // grab how much memory is used by that process + usedMem = usedMem + wkgPtr->allocatedMemSizes[memIterator]; + } + totalArraySize = totalArraySize + 1; + } + // move to the next pointer + wkgPtr = wkgPtr->nextPtr; + } -// // reset the working pointer -// wkgPtr = pcbList->headPtr; + // reset the working pointer + wkgPtr = pcbList->headPtr; -// // declare the array for the memory -// memorySizeArray = malloc(sizeof(int) * totalArraySize); + // declare the array for the memory + memorySizeArray = malloc(sizeof(int) * totalArraySize); -// // grab the entire memory structure, so that the memory can be contiguous -// while (wkgPtr != NULL) -// { -// // count up the total used memory within each process control block. -// for (memIterator = 0; memIterator < totalArraySize; memIterator++) -// { -// // grab how much memory is used by that process -// memorySizeArray[memIterator] = wkgPtr->allocatedMemSizes[memIterator]; -// } -// // move to the next pointer -// wkgPtr = wkgPtr->nextPtr; -// } + // grab the entire memory structure, so that the memory can be contiguous + while (wkgPtr != NULL) + { + // count up the total used memory within each process control block. + for (memIterator = 0; memIterator < totalArraySize; memIterator++) + { + // grab how much memory is used by that process + memorySizeArray[memIterator] = wkgPtr->allocatedMemSizes[memIterator]; + } + // move to the next pointer + wkgPtr = wkgPtr->nextPtr; + } -// // grab the size of the -// // while() -// // { -// // // -// // } + // grab the size of the + // while() + // { + // // + // } -// // switch case for each memory code -// switch (memCode) -// { -// case MEMORY_INITIALIZE: -// for(memIterator = 0; memIterator < singlePCB->memArraySize; memIterator++) -// { -// // initialize the arrays with all '-1' to 'nullify' the memory, make it easier to iterate through later -// singlePCB->allocatedMemOffsets[memIterator] = -1; -// singlePCB->allocatedMemSizes[memIterator] = -1; -// } -// if(memDisplay) -// { -// // -// printf("--------------------------------------------------\n"); -// printf("After memory initialization\n"); -// printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1); -// printf("--------------------------------------------------\n"); -// } -// break; + // switch case for each memory code + switch (memCode) + { + case MEMORY_INITIALIZE: + for(memIterator = 0; memIterator < singlePCB->memArraySize; memIterator++) + { + // initialize the arrays with all '-1' to 'nullify' the memory, make it easier to iterate through later + singlePCB->allocatedMemOffsets[memIterator] = -1; + singlePCB->allocatedMemSizes[memIterator] = -1; + } + if(memDisplay) + { + // + printf("--------------------------------------------------\n"); + printf("After memory initialization\n"); + printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1); + printf("--------------------------------------------------\n"); + } + break; -// case MEMORY_ALLOCATE: -// // check if the memory allocation can happen before storing it. -// if(size <= availableMem - usedMem + 1) -// { -// // grab the offset and size, store it, using the allocate memory function. -// subFunctionSuccess = allocMemory(singlePCB, offset, size, singlePCB->memArraySize); -// usedMem = usedMem + size; -// } -// else -// { -// // set the subfunction success to false. -// subFunctionSuccess = false; -// } -// // if the function returns true, then it successfully did the operation, display as such. -// if (memDisplay) -// { -// if(subFunctionSuccess) -// { -// // -// printf("--------------------------------------------------\n"); -// printf("After memory allocation\n"); -// printf("%d [ Used, P#: %d, %d-%d ] %d\n", offset - offset, singlePCB->uniquePid, offset, -// offset + size - 1, size - 1); -// printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1); -// printf("--------------------------------------------------\n"); -// } -// else -// { -// // -// printf("--------------------------------------------------\n"); -// printf("After memory allocation failure\n"); -// printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1); -// printf("--------------------------------------------------\n"); -// } -// } -// break; + case MEMORY_ALLOCATE: + // check if the memory allocation can happen before storing it. + if(size <= availableMem - usedMem + 1) + { + // grab the offset and size, store it, using the allocate memory function. + subFunctionSuccess = allocMemory(singlePCB, offset, size, singlePCB->memArraySize); + usedMem = usedMem + size; + } + else + { + // set the subfunction success to false. + subFunctionSuccess = false; + } + // if the function returns true, then it successfully did the operation, display as such. + if (memDisplay) + { + if(subFunctionSuccess) + { + // + printf("--------------------------------------------------\n"); + printf("After memory allocation\n"); + printf("%d [ Used, P#: %d, %d-%d ] %d\n", offset - offset, singlePCB->uniquePid, offset, + offset + size - 1, size - 1); + printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1); + printf("--------------------------------------------------\n"); + } + else + { + // + printf("--------------------------------------------------\n"); + printf("After memory allocation failure\n"); + printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1); + printf("--------------------------------------------------\n"); + } + } + break; -// case MEMORY_ACCESS: -// // printf("Used memory at current: %d\n", usedMem); -// subFunctionSuccess = accessMemory(singlePCB, offset, size); -// if (memDisplay) -// { -// if (subFunctionSuccess) -// { -// // -// printf("--------------------------------------------------\n"); -// printf("After access success\n"); -// printf("%d [ Used, P#: %d, %d-%d ] %d\n", offset - offset, singlePCB->uniquePid, offset, -// offset + size - 1, size - 1); -// printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1); -// printf("--------------------------------------------------\n"); -// } -// else -// { -// // -// printf("--------------------------------------------------\n"); -// printf("After access failure\n"); -// printf("%d [ Used, P#: %d, %d-%d ] %d\n", offset - offset, singlePCB->uniquePid, offset, -// offset + size - 1, size - 1); -// printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1); -// printf("--------------------------------------------------\n"); -// } -// } -// break; + case MEMORY_ACCESS: + // printf("Used memory at current: %d\n", usedMem); + subFunctionSuccess = accessMemory(singlePCB, offset, size); + if (memDisplay) + { + if (subFunctionSuccess) + { + // + printf("--------------------------------------------------\n"); + printf("After access success\n"); + printf("%d [ Used, P#: %d, %d-%d ] %d\n", offset - offset, singlePCB->uniquePid, offset, + offset + size - 1, size - 1); + printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1); + printf("--------------------------------------------------\n"); + } + else + { + // + printf("--------------------------------------------------\n"); + printf("After access failure\n"); + printf("%d [ Used, P#: %d, %d-%d ] %d\n", offset - offset, singlePCB->uniquePid, offset, + offset + size - 1, size - 1); + printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1); + printf("--------------------------------------------------\n"); + } + } + break; -// case MEMORY_DEALLOCATE: -// subFunctionSuccess = deallocMemory(singlePCB, singlePCB->memArraySize); -// if (memDisplay) -// { -// if (subFunctionSuccess) -// { -// // THIS SHOULD ALWAYS HAPPEN. -// printf("--------------------------------------------------\n"); -// printf("After clear process %d success\n", singlePCB->uniquePid); -// printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem - usedMem, currentOffset, currentSize, availableMem - 1); -// printf("--------------------------------------------------\n"); -// } -// else -// { -// // THIS SHOULD NEVER HAPPEN. -// printf("--------------------------------------------------\n"); -// printf("After clear process %d failure\n", singlePCB->uniquePid); -// printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem - usedMem, currentOffset, currentSize, availableMem - 1); -// printf("--------------------------------------------------\n"); -// } -// } -// break; + case MEMORY_DEALLOCATE: + subFunctionSuccess = deallocMemory(singlePCB, singlePCB->memArraySize); + if (memDisplay) + { + if (subFunctionSuccess) + { + // THIS SHOULD ALWAYS HAPPEN. + printf("--------------------------------------------------\n"); + printf("After clear process %d success\n", singlePCB->uniquePid); + printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem - usedMem, currentOffset, currentSize, availableMem - 1); + printf("--------------------------------------------------\n"); + } + else + { + // THIS SHOULD NEVER HAPPEN. + printf("--------------------------------------------------\n"); + printf("After clear process %d failure\n", singlePCB->uniquePid); + printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem - usedMem, currentOffset, currentSize, availableMem - 1); + printf("--------------------------------------------------\n"); + } + } + break; -// case MEMORY_CLEAR: -// if (memDisplay) -// { -// // this will happen regardless. Will always be cleaning up memory with dealloc. -// printf("--------------------------------------------------\n"); -// printf("After memory clear\n"); -// printf("No memory configured\n"); -// printf("--------------------------------------------------\n"); -// } -// break; + case MEMORY_CLEAR: + if (memDisplay) + { + // this will happen regardless. Will always be cleaning up memory with dealloc. + printf("--------------------------------------------------\n"); + printf("After memory clear\n"); + printf("No memory configured\n"); + printf("--------------------------------------------------\n"); + } + break; -// default: -// break; -// } -// // clear out the dynamic array -// free(memorySizeArray); -// // return if the subfunction succeeded, if applicable -// return subFunctionSuccess; -// } + default: + break; + } + // clear out the dynamic array + free(memorySizeArray); + // return if the subfunction succeeded, if applicable + return subFunctionSuccess; +} /* Name: runCPU @@ -920,17 +910,15 @@ void runProcess(ProcessControlBlock *singlePCB, ConfigDataType *configPtr, Proce singlePCB->uniquePid, command, argument, intArg, intArgTwo); logOutput(configPtr, processOutput, false, logList); // run the memory manager in allocate mode - // printf("Memory Offset: %d\n", intArg); - // printf("Memory Size: %d\n", intArgTwo); - // memManagerSuccess = memManager(pcbList, MEMORY_ALLOCATE, intArg, intArgTwo, configPtr, singlePCB); - // if(!memManagerSuccess) - // { - // // - // timeDigits = accessTimer(LAP_TIMER, timeStringy); - // sprintf(processOutput, "%f, Process: %d, failed %s %s request (%d, %d)\n", timeDigits, - // singlePCB->uniquePid, command, argument, intArg, intArgTwo); - // logOutput(configPtr, processOutput, false, logList); - // } + memManagerSuccess = memManager(pcbList, MEMORY_ALLOCATE, intArg, intArgTwo, configPtr, singlePCB); + if(!memManagerSuccess) + { + // + timeDigits = accessTimer(LAP_TIMER, timeStringy); + sprintf(processOutput, "%f, Process: %d, failed %s %s request (%d, %d)\n", timeDigits, + singlePCB->uniquePid, command, argument, intArg, intArgTwo); + logOutput(configPtr, processOutput, false, logList); + } } else if(compareString(argument, "access") == EQUIV) { @@ -939,15 +927,15 @@ void runProcess(ProcessControlBlock *singlePCB, ConfigDataType *configPtr, Proce singlePCB->uniquePid, command, argument, intArg, intArgTwo); logOutput(configPtr, processOutput, false, logList); // run the memory manager in access mode - // memManagerSuccess = memManager(pcbList, MEMORY_ACCESS, intArg, intArgTwo, configPtr, singlePCB); - // if (!memManagerSuccess) - // { - // // - // timeDigits = accessTimer(LAP_TIMER, timeStringy); - // sprintf(processOutput, "%f, Process: %d, failed %s %s request (%d, %d)\n", timeDigits, - // singlePCB->uniquePid, command, argument, intArg, intArgTwo); - // logOutput(configPtr, processOutput, false, logList); - // } + memManagerSuccess = memManager(pcbList, MEMORY_ACCESS, intArg, intArgTwo, configPtr, singlePCB); + if (!memManagerSuccess) + { + // + timeDigits = accessTimer(LAP_TIMER, timeStringy); + sprintf(processOutput, "%f, Process: %d, failed %s %s request (%d, %d)\n", timeDigits, + singlePCB->uniquePid, command, argument, intArg, intArgTwo); + logOutput(configPtr, processOutput, false, logList); + } } } workingMeta = workingMeta->nextNode; @@ -983,7 +971,7 @@ void runProcess(ProcessControlBlock *singlePCB, ConfigDataType *configPtr, Proce logOutput(configPtr, processOutput, false, logList); } // deallocate the memory for the process - // memManager(pcbList, MEMORY_DEALLOCATE, intArg, intArgTwo, configPtr, singlePCB); + memManager(pcbList, MEMORY_DEALLOCATE, intArg, intArgTwo, configPtr, singlePCB); } /* @@ -1078,7 +1066,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr) currentPid = scheduleProcess(pcbList, configPtr, loggingList); // initialize memory with the memManager - // memManager(pcbList, MEMORY_INITIALIZE, memoryOffset, memorySize, configPtr, pcbList->headPtr); + memManager(pcbList, MEMORY_INITIALIZE, memoryOffset, memorySize, configPtr, pcbList->headPtr); // loop until current process is hit for(processIterator = 0; processIterator < processCount; processIterator++) @@ -1126,7 +1114,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr) } pthread_join(cpuThread, NULL); // deallocate all the memory for each process. - // memManager(pcbList, MEMORY_CLEAR, memoryOffset, memorySize, configPtr, pcbList->headPtr); + memManager(pcbList, MEMORY_CLEAR, memoryOffset, memorySize, configPtr, pcbList->headPtr); timeDigits = accessTimer(LAP_TIMER, timeStringy); sprintf(simulatorOutput, "%f, OS: System Stop\n", timeDigits); logOutput(configPtr, simulatorOutput, false, loggingList); diff --git a/Sim04/simulator.h b/Sim04/simulator.h index 07e724a..df03988 100644 --- a/Sim04/simulator.h +++ b/Sim04/simulator.h @@ -301,8 +301,8 @@ Device Input/Device: none Device Output/Device: none Dependencies: ??? */ -// bool memManager(ProcessControlBlockLL *pcbList, MEMORY_SIGNALS memCode, int offset, int size, ConfigDataType *configPtr, -// ProcessControlBlock *singlePCB); +bool memManager(ProcessControlBlockLL *pcbList, MEMORY_SIGNALS memCode, int offset, int size, ConfigDataType *configPtr, + ProcessControlBlock *singlePCB); /* Name: runCPU