Fixed many issues with Simulator 2, trying to fix the beginning and end of CPU/IO subprocesses

+- StandardConstants.h
+- simulator.c/h
This commit is contained in:
Charles Descamps 2024-03-10 16:46:18 -07:00
parent 02cb8b33e6
commit 20a6731ee2
5 changed files with 47 additions and 28 deletions

View file

@ -22,6 +22,9 @@
//Constant to print a dash //Constant to print a dash
#define DASH '-' #define DASH '-'
// constant for equivalent strings
#define EQUIV 0
// highest printable character // highest printable character
#define HIGHEST_PRINTABLE_CHAR '~' #define HIGHEST_PRINTABLE_CHAR '~'

View file

@ -2,3 +2,4 @@
READY state from NEW state READY state from NEW state
ate ate
tate tate

Binary file not shown.

View file

@ -1,8 +1,4 @@
#include "configops.h"
#include "metadataops.h"
#include "StandardConstants.h"
#include "simulator.h" #include "simulator.h"
#include <pthread.h>
/* /*
Name: addProcess Name: addProcess
@ -17,8 +13,11 @@ Dependencies: malloc, sizeof, ???
*/ */
ProcessControlBlock *addProcess(const ProcessControlBlock source) ProcessControlBlock *addProcess(const ProcessControlBlock source)
{ {
// declare a pointer to working node
ProcessControlBlock *workingNode; ProcessControlBlock *workingNode;
// make some space that the pointer is pointing to
workingNode = malloc(sizeof(ProcessControlBlock)); workingNode = malloc(sizeof(ProcessControlBlock));
// get each value from the source, and set their values
workingNode->uniquePid = source.uniquePid; workingNode->uniquePid = source.uniquePid;
workingNode->cpuCycles = source.cpuCycles; workingNode->cpuCycles = source.cpuCycles;
workingNode->ioCycles = source.ioCycles; workingNode->ioCycles = source.ioCycles;
@ -26,6 +25,7 @@ ProcessControlBlock *addProcess(const ProcessControlBlock source)
workingNode->process_state = source.process_state; workingNode->process_state = source.process_state;
workingNode->timeLeft = source.timeLeft; workingNode->timeLeft = source.timeLeft;
workingNode->totalCycles = source.totalCycles; workingNode->totalCycles = source.totalCycles;
// make sure to set the next pointer to NULL!
workingNode->nextPtr = NULL; workingNode->nextPtr = NULL;
// return workingNode // return workingNode
return workingNode; return workingNode;
@ -114,6 +114,7 @@ ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *me
OpCodeType *wkgMetaPtr; OpCodeType *wkgMetaPtr;
ProcessControlBlock currentPCB; ProcessControlBlock currentPCB;
ProcessControlBlock *wkgPCBPtr; ProcessControlBlock *wkgPCBPtr;
OpCodeType *wkgInsPtr;
// need a variable to count how many IO cycles are in the process // need a variable to count how many IO cycles are in the process
int ioCycleCount = 0; int ioCycleCount = 0;
// need a variable to count how many CPU cycles are in the process // need a variable to count how many CPU cycles are in the process
@ -132,6 +133,7 @@ ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *me
// set the working pointer to the first part of the metadata pointer // set the working pointer to the first part of the metadata pointer
wkgMetaPtr = metadataPtr; wkgMetaPtr = metadataPtr;
wkgInsPtr = metadataPtr;
// set another working pointer to the first part of the PCB list // set another working pointer to the first part of the PCB list
wkgPCBPtr = headPointer->headPtr; wkgPCBPtr = headPointer->headPtr;
@ -144,7 +146,7 @@ ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *me
//integerArgumentTwo = wkgMetaPtr->intArg3; //integerArgumentTwo = wkgMetaPtr->intArg3;
// loop through the process list, until you hit a sys end. // loop through the process list, until you hit a sys end.
while(wkgMetaPtr->nextNode != NULL && !(compareString(argument, "end") == 0 && compareString(command, "sys") == 0)) while(wkgMetaPtr->nextNode != NULL && !(compareString(argument, "end") == EQUIV && compareString(command, "sys") == EQUIV))
{ {
integerArgumentOne = wkgMetaPtr->intArg2; integerArgumentOne = wkgMetaPtr->intArg2;
// printf("%s command, %s argument, %d\n", command, argument, integerArgumentOne); // printf("%s command, %s argument, %d\n", command, argument, integerArgumentOne);
@ -157,13 +159,13 @@ ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *me
// set the process state to PROCESS_STATE_NEW // set the process state to PROCESS_STATE_NEW
currentPCB.process_state = PROCESS_STATE_NEW; currentPCB.process_state = PROCESS_STATE_NEW;
// set the nextOpCode as a pointer to the first place metadataPtr started // set the nextOpCode as a pointer to the first place metadataPtr started
currentPCB.nextOpCode = wkgMetaPtr->nextNode; // currentPCB.nextOpCode = wkgMetaPtr->nextNode; // broken?
} }
// count number of io cycles // count number of io cycles
if(compareString(command, "dev") == 0) if(compareString(command, "dev") == EQUIV)
{ {
integerArgumentOne = wkgMetaPtr->intArg2; integerArgumentOne = wkgMetaPtr->intArg2;
if(compareString(inOutArg, "in") == 0) if(compareString(inOutArg, "in") == EQUIV)
{ {
// //
ioCycleCount = ioCycleCount + integerArgumentOne; ioCycleCount = ioCycleCount + integerArgumentOne;
@ -175,13 +177,13 @@ ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *me
} }
} }
// count number of cpu cycles // count number of cpu cycles
if ((compareString(argument, "process") == 0 && compareString(command, "cpu") == 0)) if ((compareString(argument, "process") == EQUIV && compareString(command, "cpu") == EQUIV))
{ {
integerArgumentOne = wkgMetaPtr->intArg2; integerArgumentOne = wkgMetaPtr->intArg2;
cpuCycleCount = cpuCycleCount + integerArgumentOne; cpuCycleCount = cpuCycleCount + integerArgumentOne;
} }
if ((compareString(argument, "end") == 0 && compareString(command, "app") == 0)) if ((compareString(argument, "end") == EQUIV && compareString(command, "app") == EQUIV))
{ {
// set the ioCycles from variable // set the ioCycles from variable
currentPCB.ioCycles = ioCycleCount; currentPCB.ioCycles = ioCycleCount;
@ -189,6 +191,11 @@ ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *me
currentPCB.cpuCycles = cpuCycleCount; currentPCB.cpuCycles = cpuCycleCount;
// set totalCycles from adding both IO/CPU // set totalCycles from adding both IO/CPU
currentPCB.totalCycles = ioCycleCount + cpuCycleCount; currentPCB.totalCycles = ioCycleCount + cpuCycleCount;
// currentPCB must have the starting instruction from wkgInsPtr
currentPCB.nextOpCode = wkgInsPtr->nextNode;
// set the time left from (ioCycles * ioCycleRate) + (cpuCycles * procCyclesRate) // set the time left from (ioCycles * ioCycleRate) + (cpuCycles * procCyclesRate)
// this gets the number of ms in the process. // this gets the number of ms in the process.
timeLeftCount = (ioCycleCount * configPtr->ioCycleRate) + (cpuCycleCount * configPtr->procCycleRate); timeLeftCount = (ioCycleCount * configPtr->ioCycleRate) + (cpuCycleCount * configPtr->procCycleRate);
@ -217,6 +224,7 @@ ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *me
command = wkgMetaPtr->command; command = wkgMetaPtr->command;
argument = wkgMetaPtr->strArg1; argument = wkgMetaPtr->strArg1;
inOutArg = wkgMetaPtr->inOutArg; inOutArg = wkgMetaPtr->inOutArg;
wkgInsPtr = wkgMetaPtr;
} }
// return headPointer // return headPointer
@ -233,7 +241,7 @@ Function Output/Parameters: none
Function Output/Returned: none Function Output/Returned: none
Device Input/Device: none Device Input/Device: none
Device Output/Device: none Device Output/Device: none
Dependencies: sprintf, ??? Dependencies: fprintf, ???
*/ */
void logOutput(ConfigDataType *configPtr, char *logStr, bool isEnd) void logOutput(ConfigDataType *configPtr, char *logStr, bool isEnd)
{ {
@ -241,7 +249,7 @@ void logOutput(ConfigDataType *configPtr, char *logStr, bool isEnd)
{ {
// //
FILE *fileWritePtr; FILE *fileWritePtr;
fileWritePtr = fopen(configPtr->logToFileName, "w"); fileWritePtr = fopen(configPtr->logToFileName, "w+");
if (fileWritePtr != NULL) if (fileWritePtr != NULL)
{ {
fprintf(fileWritePtr, "%s", logStr); fprintf(fileWritePtr, "%s", logStr);
@ -260,7 +268,7 @@ void logOutput(ConfigDataType *configPtr, char *logStr, bool isEnd)
// //
printf("\nSimulator will output to a file.\n"); printf("\nSimulator will output to a file.\n");
FILE *fileWritePtr; FILE *fileWritePtr;
fileWritePtr = fopen(configPtr->logToFileName, "w"); fileWritePtr = fopen(configPtr->logToFileName, "w+");
if (fileWritePtr != NULL) if (fileWritePtr != NULL)
{ {
fprintf(fileWritePtr, "%s", logStr); fprintf(fileWritePtr, "%s", logStr);
@ -287,7 +295,7 @@ Function Output/Parameters: none
Function Output/Returned: none Function Output/Returned: none
Device Input/Device: none Device Input/Device: none
Device Output/Device: none Device Output/Device: none
Dependencies: ??? Dependencies: pthreads, sprintf, logOutput, ???
*/ */
void runCPU(char *command, char *argument, int intArg, ConfigDataType *configPtr, int processId) void runCPU(char *command, char *argument, int intArg, ConfigDataType *configPtr, int processId)
{ {
@ -317,7 +325,7 @@ Function Output/Parameters: none
Function Output/Returned: none Function Output/Returned: none
Device Input/Device: none Device Input/Device: none
Device Output/Device: none Device Output/Device: none
Dependencies: ??? Dependencies: pthreads, sprintf, logOutput, ???
*/ */
void runIO(char *command, char *argument, int intArg, ConfigDataType *configPtr, int processId) void runIO(char *command, char *argument, int intArg, ConfigDataType *configPtr, int processId)
{ {
@ -342,7 +350,7 @@ Function Output/Parameters: none
Function Output/Returned: none Function Output/Returned: none
Device Input/Device: none Device Input/Device: none
Device Output/Device: none Device Output/Device: none
Dependencies: ??? Dependencies: accessTimer, compareString, runCPU, runIO, ???
*/ */
void runProcess(ProcessControlBlock *singlePCB, ConfigDataType *configPtr) void runProcess(ProcessControlBlock *singlePCB, ConfigDataType *configPtr)
{ {
@ -358,15 +366,17 @@ void runProcess(ProcessControlBlock *singlePCB, ConfigDataType *configPtr)
argument = workingMeta->strArg1; argument = workingMeta->strArg1;
intArg = workingMeta->intArg2; intArg = workingMeta->intArg2;
inOutArg = workingMeta->inOutArg; inOutArg = workingMeta->inOutArg;
while (!(compareString(command, "app") == 0 && compareString(argument, "end") == 0)) printf("\n%s is the current command in this process.\n", command);
printf("\n%s is the current argument in this process.\n", argument);
while (!(compareString(command, "app") == EQUIV && compareString(argument, "end") == EQUIV) && !(compareString(command, "sys") == EQUIV && compareString(argument, "end") == EQUIV))
{ {
// //
if (compareString(command, "cpu") == 0 && compareString(argument, "process") == 0) if (compareString(command, "cpu") == EQUIV && compareString(argument, "process") == EQUIV)
{ {
// //
runCPU(workingMeta->command, workingMeta->strArg1, workingMeta->intArg2, configPtr, singlePCB->uniquePid); runCPU(workingMeta->command, workingMeta->strArg1, workingMeta->intArg2, configPtr, singlePCB->uniquePid);
} }
if(compareString(command, "dev") == 0) if(compareString(command, "dev") == EQUIV)
{ {
// //
runIO(workingMeta->command, workingMeta->strArg1, workingMeta->intArg2, configPtr, singlePCB->uniquePid); runIO(workingMeta->command, workingMeta->strArg1, workingMeta->intArg2, configPtr, singlePCB->uniquePid);
@ -389,7 +399,7 @@ Function Output/Parameters: none
Function Output/Returned: none Function Output/Returned: none
Device Input/Device: none Device Input/Device: none
Device Output/Device: none Device Output/Device: none
Dependencies: ??? Dependencies: pthreads, accessTimer, sprintf, logOutput, runProcess, destroyProcessList, ???
*/ */
void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr) void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
{ {
@ -430,7 +440,6 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
pthread_t cpuThread; pthread_t cpuThread;
pthread_create(&cpuThread, NULL, (void *)&simThread, (void *)totalPCBTime); pthread_create(&cpuThread, NULL, (void *)&simThread, (void *)totalPCBTime);
pthread_join(cpuThread, NULL);
// //
int processTimeLeft = pcbPtr->timeLeft; int processTimeLeft = pcbPtr->timeLeft;
@ -444,7 +453,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
{ {
// needs to set from READY to RUNNING // needs to set from READY to RUNNING
pcbPtr->process_state = PROCESS_STATE_READY; pcbPtr->process_state = PROCESS_STATE_READY;
sprintf(simulatorOutput, "%f, OS: Process %d set to RUNNING state from READY state\n", accessTimer(LAP_TIMER, timeStringy), pcbPtr->uniquePid); sprintf(simulatorOutput, "%f, OS: Process %d set to RUNNING state from READY state\n\n", accessTimer(LAP_TIMER, timeStringy), pcbPtr->uniquePid);
logOutput(configPtr, simulatorOutput, false); logOutput(configPtr, simulatorOutput, false);
pthread_t runThread; pthread_t runThread;
pthread_create(&runThread, NULL, (void *)&simThread, (void *)processTimeLeft); pthread_create(&runThread, NULL, (void *)&simThread, (void *)processTimeLeft);
@ -452,7 +461,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
// runCPU(pcbPtr, configPtr); // runCPU(pcbPtr, configPtr);
runProcess(pcbPtr, configPtr); runProcess(pcbPtr, configPtr);
// when finished, set to terminated // when finished, set to terminated
sprintf(simulatorOutput, "%f, OS: Process %d set to TERMINATED state from RUNNING state\n", accessTimer(LAP_TIMER, timeStringy), pcbPtr->uniquePid); sprintf(simulatorOutput, "%f, OS: Process %d set to TERMINATED state from RUNNING state\n\n", accessTimer(LAP_TIMER, timeStringy), pcbPtr->uniquePid);
logOutput(configPtr, simulatorOutput, false); logOutput(configPtr, simulatorOutput, false);
pcbPtr->process_state = PROCESS_STATE_TERMINATED; pcbPtr->process_state = PROCESS_STATE_TERMINATED;
@ -461,11 +470,16 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
pcbPtr = pcbPtr->nextPtr; pcbPtr = pcbPtr->nextPtr;
currentPid = scheduleProcess(pcbList, configPtr); currentPid = scheduleProcess(pcbList, configPtr);
} }
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));
logOutput(configPtr, simulatorOutput, true);
pthread_join(cpuThread, NULL);
// deallocate the process list with destroyProcessList // deallocate the process list with destroyProcessList
destroyProcessList(pcbList); destroyProcessList(pcbList);
// once complete, log the output to FILE if required // once complete, log the output to FILE if required
logOutput(configPtr, "", true); logOutput(configPtr, "", true);
pthread_exit(NULL); // pthread_exit(NULL); // this line seems to make the program execution stop short... no warning either
// void function, no return. // void function, no return.
} }
@ -478,7 +492,7 @@ Function Output/Parameters: none
Function Output/Returned: none Function Output/Returned: none
Device Input/Device: none Device Input/Device: none
Device Output/Device: none Device Output/Device: none
Dependencies: ??? Dependencies: sprintf, logOutput, ???
*/ */
int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPtr) int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPtr)
{ {
@ -503,13 +517,13 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt
// //
if(wkgPtr->process_state != PROCESS_STATE_READY && wkgPtr->process_state != PROCESS_STATE_TERMINATED) if(wkgPtr->process_state != PROCESS_STATE_READY && wkgPtr->process_state != PROCESS_STATE_TERMINATED)
{ {
// // set the process within the PCB to the ready state, report in ready state.
wkgPtr->process_state = PROCESS_STATE_READY; wkgPtr->process_state = PROCESS_STATE_READY;
// printf("Process %d set to READY from NEW\n", wkgPtr->uniquePid); // printf("Process %d set to READY from NEW\n", wkgPtr->uniquePid);
sprintf(loggingStr, "%f, OS: Process %d set to READY state from NEW state\n", accessTimer(LAP_TIMER, timeStringy), wkgPtr->uniquePid); sprintf(loggingStr, "%f, OS: Process %d set to READY state from NEW state\n", accessTimer(LAP_TIMER, timeStringy), wkgPtr->uniquePid);
logOutput(configPtr, loggingStr, false); logOutput(configPtr, loggingStr, false);
} }
// move to the next pointer
wkgPtr = wkgPtr->nextPtr; wkgPtr = wkgPtr->nextPtr;
} }
@ -554,7 +568,7 @@ Function Output/Parameters: none
Function Output/Returned: none Function Output/Returned: none
Device Input/Device: none Device Input/Device: none
Device Output/Device: none Device Output/Device: none
Dependencies: ??? Dependencies: pthreads, runTimer
*/ */
void *simThread(int milSec) void *simThread(int milSec)
{ {

View file

@ -5,6 +5,7 @@
#include "configops.h" #include "configops.h"
#include "metadataops.h" #include "metadataops.h"
#include "simtimer.h" #include "simtimer.h"
#include <pthread.h>
// data structures // data structures