Savepoint, have most everything in place for the memory functions, will just need to fill them in.

+- Simulator.c
This commit is contained in:
Charles Descamps 2024-03-18 21:28:38 -07:00
parent 6446272063
commit bb6d8ac0af
3 changed files with 142 additions and 34 deletions

View file

@ -1,4 +1,4 @@
0.000000, OS: Simulator start
0.000001, OS: Simulator start
READY state from NEW state
ate

Binary file not shown.

View file

@ -5,14 +5,15 @@ Name: accessMemory
Process: checks offset from the PCB
Function Input/Parameters: source PCB (ProcessControlBlock *), offset (int), size (int)
Function Output/Parameters: none
Function Output/Returned: none?
Function Output/Returned: boolean to indicate success or failure of accessed memory
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void accessMemory(ProcessControlBlock *singlePCB, int offset, int size)
bool accessMemory(ProcessControlBlock *singlePCB, int offset, int size)
{
// no return, void (temp)
// return true (temp)
return true;
}
/*
@ -54,14 +55,16 @@ Name: allocMemory
Process: 'allocates' memory within the PCB, stores offset and size within it's PCB
Function Input/Parameters: source PCB (ProcessControlBlock *), offset (int), size (int)
Function Output/Parameters: none
Function Output/Returned: none?
Function Output/Returned: boolean to indicate success or failure of allocated memory
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void allocMemory(ProcessControlBlock *singlePCB, int offset, int size)
bool allocMemory(ProcessControlBlock *singlePCB, int offset, int size)
{
// no return, void (temp)
// singlePCB->allocatedMemOffsets[] = offset;
// return true (temp)
return true;
}
/*
@ -90,14 +93,15 @@ Name: deallocMemory
Process: 'deallocates' memory within the PCB, removes offset and size within it's PCB
Function Input/Parameters: source PCB (ProcessControlBlock *), offset (int), size (int)
Function Output/Parameters: none
Function Output/Returned: none?
Function Output/Returned: boolean to indicate success or failure of allocated memory
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void deallocMemory(ProcessControlBlock *singlePCB, int offset, int size)
bool deallocMemory(ProcessControlBlock *singlePCB, int offset, int size)
{
// no return, void (temp)
// return true (temp)
return true;
}
/*
@ -378,7 +382,7 @@ Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void memManager(ProcessControlBlockLL *pcbList, MEMORY_SIGNALS memCode, int offset, int size, ConfigDataType *configPtr)
void memManager(ProcessControlBlockLL *pcbList, MEMORY_SIGNALS memCode, int offset, int size, ConfigDataType *configPtr, ProcessControlBlock *singlePCB)
{
bool memDisplay = configPtr->memDisplay;
ProcessControlBlock *wkgPtr;
@ -387,8 +391,12 @@ void memManager(ProcessControlBlockLL *pcbList, MEMORY_SIGNALS memCode, int offs
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.
@ -396,15 +404,47 @@ void memManager(ProcessControlBlockLL *pcbList, MEMORY_SIGNALS memCode, int offs
{
// 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;
// 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 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)
{
//
@ -416,44 +456,83 @@ void memManager(ProcessControlBlockLL *pcbList, MEMORY_SIGNALS memCode, int offs
break;
case MEMORY_ALLOCATE:
// grab the offset and size, store it, using the allocate memory function.
// if the function returns true, then it successfully did the operation, display as such.
subFunctionSuccess = allocMemory(singlePCB, offset, size);
if (memDisplay)
{
//
printf("--------------------------------------------------\n");
printf("After memory allocation\n");
printf("%d [ Used, P%d: x, %d-%d ] %d\n", usedMem, wkgPtr->uniquePid, offset, size, availableMem - 1);
printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1);
printf("--------------------------------------------------\n");
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:
subFunctionSuccess = accessMemory(singlePCB, offset, size);
if (memDisplay)
{
//
printf("--------------------------------------------------\n");
printf("After access success/failure\n");
printf("%d [ Used, P%d: x, %d-%d ] %d\n", usedMem, wkgPtr->uniquePid, offset, size, availableMem - 1);
printf("%d [ Open, P#: x, %d-%d ] %d\n", usedMem, currentOffset, currentSize, availableMem - 1);
printf("--------------------------------------------------\n");
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, offset, size);
if (memDisplay)
{
//
printf("--------------------------------------------------\n");
printf("After memory deallocate\n");
printf("Deallocated memory chunk.\n");
printf("--------------------------------------------------\n");
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, 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, 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");
@ -464,6 +543,8 @@ void memManager(ProcessControlBlockLL *pcbList, MEMORY_SIGNALS memCode, int offs
default:
break;
}
// clear out the dynamic array
free(memorySizeArray);
// no return, void (temp)
}
@ -537,16 +618,19 @@ void runProcess(ProcessControlBlock *singlePCB, ConfigDataType *configPtr, Proce
// run over the entire process, running the CPU when a CPU cycle comes up
// or a run IO when an IO cycle comes up
char timeStringy[MAX_STR_LEN];
char processOutput[HUGE_STR_LEN];
// grab the next operation code for the
OpCodeType *workingMeta = singlePCB->nextOpCode;
char *command;
char *argument;
int intArg;
char *inOutArg;
int intArgTwo;
// char *inOutArg;
command = workingMeta->command;
argument = workingMeta->strArg1;
intArg = workingMeta->intArg2;
inOutArg = workingMeta->inOutArg;
intArgTwo = workingMeta->intArg3;
// inOutArg = workingMeta->inOutArg;
while (!(compareString(command, "app") == EQUIV && compareString(argument, "end") == EQUIV) && !(compareString(command, "sys") == EQUIV && compareString(argument, "end") == EQUIV))
{
// check to see what operation will be performed
@ -560,11 +644,35 @@ void runProcess(ProcessControlBlock *singlePCB, ConfigDataType *configPtr, Proce
// run IO operations for the required amount of time.
runIO(workingMeta->command, workingMeta->strArg1, workingMeta->intArg2, configPtr, singlePCB->uniquePid);
}
if(compareString(command, "mem") == EQUIV)
{
// run memory operations with the memory manager
if(compareString(argument, "allocate") == EQUIV)
{
sprintf(processOutput, "%f, Process: %d, %s %s request (%d, %d)\n", accessTimer(LAP_TIMER, timeStringy), singlePCB->uniquePid, command, argument, intArg, intArgTwo);
logOutput(configPtr, processOutput, false);
// run the memory manager in allocate mode
memManager(pcbList, MEMORY_ALLOCATE, intArg, intArgTwo, configPtr, singlePCB);
}
else if(compareString(argument, "access") == EQUIV)
{
sprintf(processOutput, "%f, Process: %d, %s %s request (%d, %d)\n", accessTimer(LAP_TIMER, timeStringy), singlePCB->uniquePid, command, argument, intArg, intArgTwo);
logOutput(configPtr, processOutput, false);
// run the memory manager in access mode
memManager(pcbList, MEMORY_ACCESS, intArg, intArgTwo, configPtr, singlePCB);
}
}
workingMeta = workingMeta->nextNode;
command = workingMeta->command;
argument = workingMeta->strArg1;
intArg = workingMeta->intArg2;
intArgTwo = workingMeta->intArg3;
}
// display the process exited
sprintf(processOutput, "\n%f, OS: Process %d ended\n", accessTimer(LAP_TIMER, timeStringy), singlePCB->uniquePid);
logOutput(configPtr, processOutput, false);
// deallocate the memory for the process
memManager(pcbList, MEMORY_DEALLOCATE, intArg, intArgTwo, configPtr, singlePCB);
}
/*
@ -582,7 +690,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
{
// initialize function/variables
// need two working pointers, one for master loop, one for internal loop
OpCodeType *wkgPtrMaster;
// OpCodeType *wkgPtrMaster;
// currentTime variable that stores the time. [now unsure if needed]
// need a variable for headPointer to point
ProcessControlBlockLL *pcbList;
@ -630,7 +738,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
currentPid = scheduleProcess(pcbList, configPtr);
// initialize memory with the memManager
memManager(pcbList, MEMORY_INITIALIZE, memoryOffset, memorySize, configPtr);
memManager(pcbList, MEMORY_INITIALIZE, memoryOffset, memorySize, configPtr, pcbList->headPtr);
// loop until current process is hit
for(processIterator = 0; processIterator < processCount; processIterator++)
@ -663,7 +771,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);
memManager(pcbList, MEMORY_CLEAR, memoryOffset, memorySize, configPtr, pcbList->headPtr);
sprintf(simulatorOutput, "%f, OS: System Stop\n", accessTimer(LAP_TIMER, timeStringy));
logOutput(configPtr, simulatorOutput, false);
sprintf(simulatorOutput, "%f, OS: Simulation End\n", accessTimer(LAP_TIMER, timeStringy));