diff --git a/Sim03/config0.cnf b/Sim03/config0.cnf index 08f2bfd..9404729 100644 --- a/Sim03/config0.cnf +++ b/Sim03/config0.cnf @@ -1,7 +1,7 @@ Start Simulator Configuration File: Version/Phase : 1.05 File Path : metadata5.mdf -CPU Scheduling Code : FCFS-N +CPU Scheduling Code : SJF-N Quantum Time (cycles) : 3 Memory Available (KB) : 11100 Memory Display (On/Off) : On diff --git a/Sim03/logfile_1.lgf b/Sim03/logfile_1.lgf index 52b0e91..91abba2 100644 --- a/Sim03/logfile_1.lgf +++ b/Sim03/logfile_1.lgf @@ -1,7 +1,6 @@ -0.000000, OS: Simulator start +0.000001, OS: Simulator start READY state from NEW state ate ate - diff --git a/Sim03/sim03 b/Sim03/sim03 new file mode 100755 index 0000000..b640cd6 Binary files /dev/null and b/Sim03/sim03 differ diff --git a/Sim03/simulator.c b/Sim03/simulator.c index ea7e6e0..21770f3 100644 --- a/Sim03/simulator.c +++ b/Sim03/simulator.c @@ -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 diff --git a/Sim03/simulator.h b/Sim03/simulator.h index 0a07783..4ccba46 100644 --- a/Sim03/simulator.h +++ b/Sim03/simulator.h @@ -131,6 +131,18 @@ Dependencies: ??? */ ProcessControlBlockLL *fillProcessList(ConfigDataType *configPtr, OpCodeType *metadataPtr, ProcessControlBlockLL *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); + /* Name: logOutput Process: Takes in a string, and then outputs it to monitor AND/OR file