Starting work on Simulator 3, SJF works-ish...

+- config0.cnf (SJF mode)
+- simulator.c/h
+- sim03
This commit is contained in:
Charles Descamps 2024-03-13 15:27:58 -07:00
parent 90c3f8cc3f
commit 7035f119a8
5 changed files with 87 additions and 8 deletions

View file

@ -227,6 +227,27 @@ ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *me
return headPointer;
}
/*
Name: findMin
Process: compares two values, returns the smaller of the two.
Function Input/Parameters: two integer values (int)
Function Output/Parameters: none
Function Output/Returned: minimum of the two input integer values (int)
Device Input/Device: none
Device Output/Device: none
Dependencies: none
*/
int findMin(int one, int another)
{
int minimumVal = one;
if(another < minimumVal)
{
minimumVal = another;
}
// return zero(temp)
return minimumVal;
}
/*
Name: logOutput
Process: Takes in a string, and then outputs it to monitor AND/OR file
@ -445,7 +466,7 @@ void runSim(ConfigDataType *configPtr, OpCodeType *metaDataMstrPtr)
if(pcbPtr->uniquePid == currentPid)
{
// needs to set from READY to RUNNING
pcbPtr->process_state = PROCESS_STATE_READY;
pcbPtr->process_state = PROCESS_STATE_RUNNING;
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);
pthread_t runThread;
@ -497,7 +518,6 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt
{
case CPU_SCHED_FCFS_N_CODE:
// First Come, First Served, Non-preemptive Scheduling
// only one implemented for now. returns first process without fail.
// need to find whichever process is first, AND the state is NEW.
// will set the state to READY, return the pid of the process which
// was set to ready.
@ -506,7 +526,7 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt
while(wkgPtr != NULL)
{
//
if(wkgPtr->process_state != PROCESS_STATE_READY && wkgPtr->process_state != PROCESS_STATE_TERMINATED)
if(wkgPtr->process_state == PROCESS_STATE_NEW)
{
// set the process within the PCB to the ready state, report in ready state.
wkgPtr->process_state = PROCESS_STATE_READY;
@ -523,7 +543,7 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt
while(wkgPtr != NULL && (returnPid == -1))
{
// check if the process is NOT terminated
if (wkgPtr->process_state == PROCESS_STATE_READY)
if (wkgPtr->process_state != PROCESS_STATE_RUNNING && wkgPtr->process_state != PROCESS_STATE_TERMINATED)
{
//printf("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, timeStringy), wkgPtr->uniquePid, wkgPtr->timeLeft);
@ -541,7 +561,55 @@ int scheduleProcess(ProcessControlBlockLL *headPointer, ConfigDataType *configPt
// TODO: Round Robin Scheduling
break;
case CPU_SCHED_SJF_N_CODE:
// TODO: Shortest Job First Scheduling
// Shortest Job First Scheduling
// need to find shortest job, maybe using a find min function?
// reuse the previous code to set each process to READY from NEW
// will select the process that is in the READY state, with the shortest time.
int shortestJob = 0;
int shortestJobProcNum = 0;
while (wkgPtr != NULL)
{
//
if (wkgPtr->process_state == PROCESS_STATE_NEW)
{
// set the process within the PCB to the ready state, report in ready state.
wkgPtr->process_state = PROCESS_STATE_READY;
// 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);
logOutput(configPtr, loggingStr, false);
}
// move to the next pointer
wkgPtr = wkgPtr->nextPtr;
}
// reset the pointer to get ready for selection of process
wkgPtr = headPointer->headPtr;
// load in the first process time
shortestJob = wkgPtr->timeLeft;
// move to the next pointer.
wkgPtr = wkgPtr->nextPtr;
while (wkgPtr != NULL && (returnPid == -1))
{
// check if the process is NOT terminated
// now, ALSO needs to check which process takes the least time.
if (wkgPtr->process_state != PROCESS_STATE_RUNNING && wkgPtr->process_state != PROCESS_STATE_TERMINATED)
{
// check to see if the working pointer job is shorter than the current shortest job
shortestJob = findMin(shortestJob, wkgPtr->timeLeft);
// check to see if the shortest job time changed, if it did, change the process number
if(shortestJob == wkgPtr->timeLeft)
{
// change process number, or leave it unchanged.
shortestJobProcNum = wkgPtr->uniquePid;
}
}
// move to next pointer
wkgPtr = wkgPtr->nextPtr;
}
// printf("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, timeStringy), shortestJobProcNum, (double)shortestJob);
logOutput(configPtr, loggingStr, false);
returnPid = shortestJobProcNum;
break;
case CPU_SCHED_SRTF_P_CODE:
// TODO: Shortest Run-time First Scheduling