Sim02 somewhat working? Unclear as of this commit.

+- simulator.c/h
+- simtimer.c/h
This commit is contained in:
Charles Descamps 2024-02-22 02:36:52 -07:00
parent 152e4429af
commit 79db35ade5
5 changed files with 168 additions and 45 deletions

Binary file not shown.

View file

@ -30,7 +30,7 @@
const char RADIX_POINT = '.';
const char SPACED = ' ';
void *runTimer( int milliSeconds )
void runTimer( int milliSeconds )
{
struct timeval startTime, endTime;
int startSec, startUSec, endSec, endUSec;
@ -62,7 +62,7 @@ void *runTimer( int milliSeconds )
secDiff = ( endSec - startSec ) * 1000;
timeDiff = secDiff + mSecDiff;
}
return NULL;
// return NULL;
}
double accessTimer( int controlCode, char *timeStr )

View file

@ -35,7 +35,7 @@ extern const char SPACED;
// Function Prototyp /////////////////////////////////////////////////////////
void *runTimer( int milliSeconds );
void runTimer( int milliSeconds );
// will run a timer for the amount of ms specified
double accessTimer( int controlCode, char *timeStr );
// returns the actual time as a double AND as a time string

View file

@ -107,7 +107,7 @@ Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
ProcessControlBlockLL *fillProcessList(OpCodeType *metadataPtr, ProcessControlBlockLL *headPointer)
ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *metadataPtr, ProcessControlBlockLL *headPointer)
{
// initialize function/variables
// need a working pointer for the metadata
@ -202,6 +202,7 @@ ProcessControlBlockLL *fillProcessList(OpCodeType *metadataPtr, ProcessControlBl
currentPCB.totalCycles = ioCycleCount + cpuCycleCount;
// set the time left from (ioCycles * ioCycleRate) + (cpuCycles * procCyclesRate)
// this gets the number of ms in the process.
timeLeftCount = (ioCycleCount * configPtr->ioCycleRate) + (cpuCycleCount * configPtr->procCycleRate);
currentPCB.timeLeft = timeLeftCount;
// increment the PID counter
processId = processId + 1;
@ -232,7 +233,7 @@ ProcessControlBlockLL *fillProcessList(OpCodeType *metadataPtr, ProcessControlBl
inOutArg = wkgMetaPtr->inOutArg;
}
// return null (temp) (should return the headpointer)
// return headPointer
return headPointer;
}
@ -250,24 +251,91 @@ Dependencies: sprintf, ???
*/
void logOutput(ConfigDataType *configPtr, char *logStr, bool isEnd)
{
if(configPtr->logToCode == LOGTO_BOTH_CODE)
{
//
FILE *fileWritePtr;
fileWritePtr = fopen(configPtr->logToFileName, "w");
if(fileWritePtr != NULL)
if (fileWritePtr != NULL)
{
//
fprintf(fileWritePtr, "%s", logStr);
// fputs(fileWritePtr, logStr);
fprintf(stdout, "%s", logStr);
}
// no return, void
// close the file, if at the end.
// if(isEnd)
// {
// //
// fclose(fileWritePtr);
// }
if (isEnd)
{
//
fclose(fileWritePtr);
}
// fclose(fileWritePtr);
}
else if(configPtr->logToCode == LOGTO_FILE_CODE)
{
//
printf("\nSimulator will output to a file.\n");
FILE *fileWritePtr;
fileWritePtr = fopen(configPtr->logToFileName, "w");
if (fileWritePtr != NULL)
{
//
fprintf(fileWritePtr, "%s", logStr);
// fputs(fileWritePtr, logStr);
}
// no return, void
// close the file, if at the end.
if (isEnd)
{
//
fclose(fileWritePtr);
}
// fclose(fileWritePtr);
}
else
{
//
fprintf(stdout, "%s", logStr);
}
}
/*
Name: runCPU
Process: runs the CPU operations for however long it takes
Function Input/Parameters: ???
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void runCPU();
/*
Name: runIO
Process: runs the IO operations for however long it takes
Function Input/Parameters: ???
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void runIO();
/*
Name: runProcess
Process: runs the process that was selected
Function Input/Parameters: ???
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void runProcess();
/*
Name: runSim
Process: primary simulation driver
@ -291,10 +359,11 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
// string variable for nextCode, temp unused
// char *nextCode;
// string variable for the timer stuff
char *simulatorOutput = "Simulator Start\n";
char simulatorOutput[MAX_STR_LEN];
// accessTimer(ZERO_TIMER, NULL);
// Operating System start timer goes here, log it.
// sprintf(simulatorOutput, "Simulator start\n");
sprintf(simulatorOutput, "%f, OS: Simulator start\n", accessTimer(LAP_TIMER, NULL));
logOutput(configPtr, simulatorOutput, false);
// int variable for process to run
@ -302,7 +371,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
// create the process list with createProcessList, store the pointed to address
pcbList = createProcessList();
// fill the process list given the metadata
pcbList = fillProcessList(metaDataMstrPtr, pcbList);
pcbList = fillProcessList(configPtr, metaDataMstrPtr, pcbList);
pcbPtr = pcbList->headPtr;
// create temp strings
char *command;
@ -320,25 +389,13 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
}
pcbPtr = pcbList->headPtr;
pthread_t cpuThread;
pthread_create(&cpuThread, NULL, (void *)&simThread, (void *)totalPCBTime);
pthread_join(cpuThread, NULL);
// set the current process id to the first scheduled process
currentPid = scheduleProcess(pcbList, configPtr);
// function holds master loop, loops until sys end opcode found OR wkgPtrMaster nextNode != NULL
// while(wkgPtrMaster->nextNode != NULL && !(compareString(argument, "end") == 0 && compareString(command, "sys") == 0))
// {
// currentPid = scheduleProcess(pcbList, configPtr);
// // will have a loop that goes from app start to app end for a PCB
// // (checks to see if the next instruction is NOT app end), using getNextProcess?
// // this will getNextOpCode
// // this will checkForIO and run the IO operations, outputting to FILE * and/or monitor
// // checkForIO will need the FILE * passed, will need timer access, and ???
// // this will checkForCPU and run the CPU operations, outputting to FILE * and/or monitor
// // checkForCPU will need the FILE * passed, will need timer access, and ???
// // move the wkgPtrMaster to the next metadata block
// wkgPtrMaster = wkgPtrMaster->nextNode;
// // set the command and argument to the current cmd and arg
// command = wkgPtrMaster->command;
// argument = wkgPtrMaster->strArg1;
// }
// loop until current process is hit
while(pcbPtr != NULL)
{
@ -346,10 +403,14 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
if(pcbPtr->uniquePid == currentPid)
{
//
printf("Process %d running", currentPid);
// printf("Process %d running\n", currentPid);
// needs to set from READY to RUNNING
// when finished, set to terminated
pcbPtr->process_state = PROCESS_STATE_TERMINATED;
}
pcbPtr = pcbPtr->nextPtr;
currentPid = scheduleProcess(pcbList, configPtr);
}
//
@ -357,6 +418,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
destroyProcessList(pcbList);
// once complete, log the output to FILE if required
logOutput(configPtr, "", true);
pthread_exit(NULL);
// void function, no return.
}
@ -376,7 +438,7 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt
ProcessControlBlock *wkgPtr;
int returnPid = -1;
wkgPtr = headPointer->headPtr;
char *loggingStr;
char loggingStr[MAX_STR_LEN];
// switch case for each scheduling algorithm
switch (configPtr->cpuSchedCode)
{
@ -389,12 +451,17 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt
// set every process in the list to READY from NEW
while(wkgPtr != NULL)
{
//
if(wkgPtr->process_state != PROCESS_STATE_READY && wkgPtr->process_state != PROCESS_STATE_TERMINATED)
{
//
wkgPtr->process_state = PROCESS_STATE_READY;
//printf("Process %d set to READY from NEW\n", wkgPtr->uniquePid);
sprintf(loggingStr, "Process %d set to READY state from NEW state\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, NULL), wkgPtr->uniquePid);
logOutput(configPtr, loggingStr, false);
}
wkgPtr = wkgPtr->nextPtr;
}
@ -406,7 +473,7 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt
if (wkgPtr->process_state == PROCESS_STATE_READY)
{
//printf("Process %d Selected with %.0f ms remaining\n", wkgPtr->uniquePid, wkgPtr->timeLeft);
sprintf(loggingStr, "Process %d selected with %.0f ms remaining\n", wkgPtr->uniquePid, wkgPtr->timeLeft);
sprintf(loggingStr, "%f, OS: Process %d selected with %.0f ms remaining\n", accessTimer(LAP_TIMER, NULL), wkgPtr->uniquePid, wkgPtr->timeLeft);
logOutput(configPtr, loggingStr, false);
returnPid = wkgPtr->uniquePid;
}
@ -430,6 +497,26 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt
// return returnPid
return returnPid;
}
/*
Name: simThread
Process: wrapper functions for threading in the simulator
Function Input/Parameters: milliseconds (int)
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void *simThread(int milSec)
{
//
// int *milSecPtr = (int *)milSec;
// printf("Running Timer for %d ms, %d sec\n", milSec, milSec / 1000);
runTimer(milSec);
// printf("Ran Timer\n");
pthread_exit(NULL);
}
/*
Current Note to programmer: do the scheduler, the runIO and runCPU functions, and then the FILE IO functions
this is all that is left so far, hopefully not too bad.

View file

@ -141,7 +141,7 @@ Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
ProcessControlBlockLL *fillProcessList(OpCodeType *metadataPtr, ProcessControlBlockLL *headPointer);
ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *metadataPtr, ProcessControlBlockLL *headPointer);
/*
Name: logOutput
@ -157,6 +157,30 @@ Dependencies: sprintf, ???
*/
void logOutput(ConfigDataType *configPtr, char *logStr, bool isEnd);
/*
Name: runCPU
Process: runs the CPU operations for however long it takes
Function Input/Parameters: ???
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void runCPU();
/*
Name: runIO
Process: runs the IO operations for however long it takes
Function Input/Parameters: ???
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void runIO();
/*
Name: runSim
Process: primary simulation driver
@ -182,4 +206,16 @@ Dependencies: ???
*/
int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPtr);
/*
Name: simThread
Process: wrapper functions for threading in the simulator
Function Input/Parameters: milliseconds (int)
Function Output/Parameters: none
Function Output/Returned: none
Device Input/Device: none
Device Output/Device: none
Dependencies: ???
*/
void *simThread(int milSec);
#endif // SIMULATORX