0% found this document useful (0 votes)
444 views

Shortest Remaining Time First (SRTF) Scheduling Algorithm

The document discusses the Shortest Remaining Time First (SRTF) scheduling algorithm. It describes the algorithm, provides pseudocode for its implementation, and includes sample output and discussion.

Uploaded by

Yi Yin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
444 views

Shortest Remaining Time First (SRTF) Scheduling Algorithm

The document discusses the Shortest Remaining Time First (SRTF) scheduling algorithm. It describes the algorithm, provides pseudocode for its implementation, and includes sample output and discussion.

Uploaded by

Yi Yin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

University Malaysia Terengganu

CSF3305
Operating System

Group 17: Shortest Remaining


Time First (SRTF) Scheduling
Algorithm

Author: Matric Number:


YIEN QIAN XIANG S58901
KHOR KEAN KAI S60801
LAM JING KIT S59387
TEOH YI YIN S58798
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

Contents
1 Introduction 2

2 Advantages and Disadvantages 3


2.1 Advantages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Disadvantages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

3 Source Code 4

4 Output and Discussion 14


4.1 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.2 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

5 Conclusion 17

1
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

1 Introduction
Shortest remaining time first (SRTF) is a scheduling algorithm which is a preemptive
version of Shortest Job First (SJF) scheduling. Since it follows the SJF scheduling,
the arrangement of the process is based on the time needed to complete the execu-
tion. In this scheduling algorithm, the processor with the closest time remaining to
completion will be allocated to execute first.
SRTF algorithm is a preemptive scheduling so the current process execution
can be preempted by another process if the new arrival process has a shorter time
to complete its execution. So, the SRTF algorithm is more efficient than the SJF
scheduling due to its ability to allow preemption.
The process execution can be stopped at anytime when a new process ar-
rives into the ready queue in the SRTF scheduling algorithm. The process will be
compared with the new process after its arrival and the process that has the shorter
remaining burst time will be chosen to continue the execution process.

2
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

2 Advantages and Disadvantages


2.1 Advantages
• SRTF scheduling algorithm allows preemption, hence the waiting time of the
processes reduce.
• Algorithm only needs to compare the currently executing process with the
newly added process.
• SRTF scheduling algorithm’s overhead charges are not counted since it only
makes a decision when a process is completely executed or a new process is
added.
• Short processes executed in a very short time, hence throughput increased.

2.2 Disadvantages
• Long processes may be held off indefinitely if short processes continually added
and may lead to process starvation.
• Cannot predict burst time and the amount of CPU time a job has left, therefore
impossible to implement SRTF scheduling algorithm in an interactive system.
• The context switching time increased due to more consumption of the CPU’s
valuable time for processing.

3
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

3 Source Code
Main Class
package SRTF;

public class SRTFtest {


public static void main(String[] args) {
ShortestRemaindingTimeFirst test = new
ShortestRemaindingTimeFirst();
}
}

ShortestRemaindingTimeFirst Class
package SRTF;

import java.util.*;
public class ShortestRemaindingTimeFirst {

private Process[] process;


private int processNumber = 0;
private int runTime = 0;
private int ShortestRemainingTime;
private int ShortestProcess;
private boolean isFinish = false;
private double TotalTurnAround = 0;
private double TotalWaiting = 0;
private double AverageTurnAround;
private double AverageWaiting;

public ShortestRemaindingTimeFirst() {
boolean isInteger = false;

//Input for Process Attribution


do {
try {
Scanner input = new Scanner(System.in);
//Here get Process Number
if (this.getProcessNumber() <= 0) {

System.out.print("Please enter the total number of


processes: ");

4
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

this.setProcessNumber(input.nextInt());
if (this.getProcessNumber() == 0) {
throw new ZeroException();
}
else if (this.getProcessNumber() < 0) {
throw new NegativeValueException();
}

this.process = new Process[this.getProcessNumber()];


}

//Here get the Process Burst Time and Arrival Time


for (int i = 0; i < this.getProcessNumber(); i++) {
int temporarilyArrival;
int temporarilyBurst;

System.out.print("\nArrival Time for Process " + (i +


1) + ": ");
temporarilyArrival = input.nextInt();
if (temporarilyArrival < 0) {
throw new NegativeValueException();
}

System.out.print("Burst Time for Process " + (i + 1) +


": ");
temporarilyBurst = input.nextInt();
if (temporarilyBurst == 0) {
throw new ZeroException();
}
else if (temporarilyBurst < 0) {
throw new NegativeValueException();
}
process[i] = new Process((i + 1), temporarilyArrival,
temporarilyBurst);
}
isInteger = true;
} catch (InputMismatchException | NumberFormatException e) {
System.err.printf("Exception: %s \n", e);
System.out.println("Please enter integer only.");
} catch (ZeroException a) {
System.err.printf("Exception: %s \n", a);
} catch (NegativeValueException b) {
System.err.printf("Exception: %s \n", b);

5
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

}
} while (!isInteger);
System.out.println("\n\nProcess Execution Sequence:");

//Here start the SRT algorithm


int previousExecutedProcess = 0;
while (!this.isFinish) {

this.setShortestProcess(-1);

//Here Checking for Arrived Process


for (int i = 0; i < this.getProcessNumber(); i++) {
if (!process[i].getIsComplete()) {
if (process[i].getArrivalTime() <= this.getRunTime()) {
process[i].setIsArrive(true);
this.setShortestRemainingTime(process[i].getRemainingTime());
}
}
}
//Here Select the Arrived Shortest Remainding Time Process
int count = 0;
for (int j = this.getProcessNumber() - 1; j >= 0; j--) {
if ((process[j].getIsArrive() == true) &&
(process[j].getIsComplete() == false)) {
if (process[j].getRemainingTime() <=
this.getShortestRemainingTime()) {
this.setShortestProcess(j);
this.setShortestRemainingTime(process[j].getRemainingTime());
}
}
else if (!process[j].getIsArrive()) {
count++;
}
}
if ((process[previousExecutedProcess].getIsArrive() == true)
&& (process[previousExecutedProcess].getIsComplete() ==
false)) {
if (process[previousExecutedProcess].getRemainingTime() ==
this.getShortestRemainingTime()) {
this.setShortestProcess(previousExecutedProcess);
this.setShortestRemainingTime(process[previousExecutedProcess].getRemain
}
}

6
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

//Here start to add 1 second to the run time


this.setRunTime(this.getRunTime() + 1);

//Here start to execute the Arrived Shortest Remainding Time


Process
boolean intPrintOnce = true; //for printing null once

for (int k = 0; k < this.getProcessNumber(); k++) {


if (count != this.getProcessNumber() &&
this.getShortestProcess() != -1) {
if ((process[k].getIsArrive()) &&
(!process[k].getIsComplete())) {
if (k == (this.getShortestProcess())) {
process[k].setRemainingTime((process[k].getRemainingTime()
- 1));
System.out.printf("P%d ",
process[k].getProcessID());
previousExecutedProcess = k;

if (process[k].getRemainingTime() == 0) {
process[k].setIsComplete(true);
process[k].setCompletionTime(this.getRunTime());
}
}
else {
process[k].setWaitingTime(process[k].getWaitingTime()
+ 1);
}
}
}
else if (this.getShortestProcess() == -1) {
if ((process[k].getIsArrive()) &&
(!process[k].getIsComplete())) {
process[k].setWaitingTime(process[k].getWaitingTime()
+ 1);
}
if (intPrintOnce) {
intPrintOnce = false;
System.out.print("P*");
}
}
}

7
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

//Last part checking whether all process finish


this.isFinish = true;
for (int j = 0; j < this.getProcessNumber(); j++) {
if (!process[j].getIsComplete()) {
this.isFinish = false;
}
}
}

System.out.println("\n\nP* = no process execute.");


System.out.println();

//Calculate TurnAround time ,Average TurnAround time and Average


Waiting Time
for (int m = 0; m < this.getProcessNumber(); m++) {
process[m].calculateTurnAroundTime();
this.setTotalTurnAround(this.getTotalTurnAround() +
process[m].getTurnAroundTime());
this.setTotalWaiting(this.getTotalWaiting() +
process[m].getWaitingTime());
}

this.calculateAverageTurnAround(this.getTotalTurnAround());
this.calculateAverageWaiting(this.getTotalWaiting());

//Display all the information


System.out.println("\n-------------------------------------------------------------
System.out.println("Process Burst Arrival Time Burst Time
Waiting Time Turnaround Time");
System.out.println("---------------------------------------------------------------
for (int m = 0; m < this.getProcessNumber(); m++) {
System.out.printf("P%-15d %-15d %-13d %-15d %d\n",
process[m].getProcessID(), process[m].getArrivalTime(),
process[m].getBurstTime(), process[m].getWaitingTime(),
process[m].getTurnAroundTime());
}
System.out.printf("Average %38s %-15.2f %.2f", "",
this.getAverageWaiting(), this.getAverageTurnAround());
System.out.println("\n-------------------------------------------------------------
}

public int getProcessNumber() {

8
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

return processNumber;
}

public void setProcessNumber(int processNumber) {


this.processNumber = processNumber;
}

public int getRunTime() {


return runTime;
}

public void setRunTime(int runTime) {


this.runTime = runTime;
}

public int getShortestRemainingTime() {


return ShortestRemainingTime;
}

public void setShortestRemainingTime(int ShortestRemainingTime) {


this.ShortestRemainingTime = ShortestRemainingTime;
}

public int getShortestProcess() {


return ShortestProcess;
}

public void setShortestProcess(int ShortestProcess) {


this.ShortestProcess = ShortestProcess;
}

public double getAverageTurnAround() {


return AverageTurnAround;
}

public void calculateAverageTurnAround(double totalTurnAround) {


this.AverageTurnAround = (totalTurnAround /
this.getProcessNumber());
}

public double getAverageWaiting() {


return AverageWaiting;
}

9
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

public void calculateAverageWaiting(double totalWaiting) {


this.AverageWaiting = (totalWaiting / this.getProcessNumber());
}

public double getTotalTurnAround() {


return TotalTurnAround;
}

public void setTotalTurnAround(double TotalTurnAround) {


this.TotalTurnAround = TotalTurnAround;
}

public double getTotalWaiting() {


return TotalWaiting;
}

public void setTotalWaiting(double TotalWaiting) {


this.TotalWaiting = TotalWaiting;
}
}

Process Class
package SRTF;

public class Process {


private int ProcessID;
private int ArrivalTime;
private int BurstTime;
private int RemainingTime;
private int CompletionTime;
private int WaitingTime = 0;
private int TurnAroundTime;
private boolean isArrive = false;
private boolean isComplete = false;

public Process(){}

public Process(int ID, int ArrivalTime, int BurstTime){


this.setProcessID(ID);
this.setArrivalTime(ArrivalTime);
this.setBurstTime(BurstTime);

10
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

this.setRemainingTime(BurstTime);
}

public int getProcessID() {


return ProcessID;
}

public void setProcessID(int ProcessID) {


this.ProcessID = ProcessID;
}

public int getArrivalTime() {


return ArrivalTime;
}

public void setArrivalTime(int ArrivalTime) {


this.ArrivalTime = ArrivalTime;
}

public int getBurstTime() {


return BurstTime;
}

public void setBurstTime(int BurstTime) {


this.BurstTime = BurstTime;
}

public int getCompletionTime() {


return CompletionTime;
}

public void setCompletionTime(int CompletionTime) {


this.CompletionTime = CompletionTime;
}

public int getWaitingTime() {


return WaitingTime;
}

public void setWaitingTime(int WaitingTime) {


this.WaitingTime = WaitingTime;
}

11
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

public boolean getIsArrive() {


return isArrive;
}

public void setIsArrive(boolean isArrive) {


this.isArrive = isArrive;
}

public boolean getIsComplete() {


return isComplete;
}

public void setIsComplete(boolean isComplete) {


this.isComplete = isComplete;
}

public int getRemainingTime() {


return RemainingTime;
}

public void setRemainingTime(int RemainingTime) {


this.RemainingTime = RemainingTime;
}

public int getTurnAroundTime() {


return TurnAroundTime;
}

public void calculateTurnAroundTime(){


this.TurnAroundTime = this.getCompletionTime() -
this.getArrivalTime();
}
}

NegativeValueException Class
package SRTF;

public class NegativeValueException extends Exception{


public NegativeValueException(){
super("Please enter non-negative integer");
}
}

12
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

ZeroException Class
package SRTF;

public class ZeroException extends Exception{


public ZeroException(){
super("Please insert any integer bigger than 0");
}
}

13
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

4 Output and Discussion


4.1 Output

Figure 1: Output of SRTF scheduling algorithm.

14
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

4.2 Discussion
The program code is constructed using Object-Oriented Programming approach.
In the main class, it instantiate the object of ShortestRemainingTimeFirst class
while the main process of SRTF scheduling algorithm is mainly carry out in the
ShortestRemainingTimeFirst class.
Firstly, in ShortestRemainingTimeFirst class, from line 22 to line 70, the
program ask the user to enter the total number of processes, then the program will
check the number entered is a valid number. If it is valid, it will be assigned as the
length of the array named Process. Next, the program will ask the user to enter
Arrival Time and Burst Time for each of the process, and keep these value in Process
array according to the index. For any exception or invalid input, the program will
catch and ask to input again.
If all input are valid, then the program will start the SRTF scheduling. The
process will go through a while loop if there are at least one process has not finish
it’s execution. From line 73 to line 105, the program go through some for loop to
determine the shortest remaining time. The determination go through 3 compari-
son, first by compare the current process’ remaining time and run time, second by
compare the last process’ remaining time and the current shortest remaining time
and the third is by compare the previous executing process and the current shortest
remaining time.
At line 108, the run time will increased by 1 whenever the program goes
through the while loop once. Started from line 111 to line 140, the program start
executing the arrived process with the shortest remaining time. If current process is
the process with shortest remaining time, it will be executed and it’s remaining time
is decreased by one and it will be displayed with respective process ID as a prove of
this process is executed at this run time. Else, it’s waiting time will be increased by
one. From line 120 to line 124, there are to check whether that particular process
has fully executed.
Next, from line 143 to line 148 is to check whether are all process have already
finish executed. If there are at least one process have remaining time value of more
than or equal to 1, then the program will iterate again via the while loop.
Later, from line 155 to line 159, there are to calculate the total turnaround
time and total waiting time of each process. The result are obtained by iterate
through a for loop which is to access to each of the process element in the Process
array and calculate their turnaround time and waiting time.
Lastly, from line 165 to line 174, there are to display all the results includ-
ing the process burst and it’s respective arrival time, burst time, waiting time,

15
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

turnaround time in table form. At the last row of the table, average waiting time
and turnaround time will be calculated and displayed too.
This program code demonstrate the preemptive property of SRTF scheduling
algorithm. This is because the process will be switched from running state to ready
state if it’s CPU burst time remaining is not the shortest among other process, or
the process will be switched from waiting state to ready state if it has the shortest
CPU burst time remaining.
Moreover, process 1 (P1) which arrive first undergo starvation due to it has
the longest CPU burst time, hence longest remaining time while other process with
shorter CPU burst time are executed completely during P1 starvation. This shows
that SRTF scheduling algorithm can reduce the average packet delay because process
with shorter CPU burst time which had already arrived can be executed without
waiting for too long. Hence, the average turnaround time could be decrease. How-
ever, SRTF scheduling algorithm sacrifice the load balancing because the process
with longest CPU burst time or most load had to starve until all lighter load process
finish their execution.

16
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

5 Conclusion
In a nutshell, the Shortest Remaining Time First (SRTF) Scheduling Algorithm
can be concluded as one of the greatest CPU algorithms that make CPU run most
efficiently due to its properties that allow preemptive use. However, it is suitable for
systems that need fast throughput but not suitable for systems that follow priority.
The output shows that SRTF Scheduling Algorithm can minimize the waiting
time of the process by choosing the process with closest time remaining to completion
and allow preemptive action after a process is completely executed. These properties
maximize the throughput but at the same time the larger process will be replaced by
the shorter process. If shorter processes keep entering the system, the larger process
will be postponed indefinitely.
In real life, the Shortest Remaining Time First Scheduling Algorithm will be
normally used on systems that need to maximize efficiency such as lift systems. As
an example, person A enters the lift on the first floor and presses to go to the tenth
floor. After that, another person B at third floor asks to enter the lift and presses
to go to the eighth floor. According to the SRTF Scheduling Algorithm, the lift will
stop at third floor to let person B enter and eighth floor to let that person B leave
and lastly open at tenth floor to let person A leave.

17
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm

References
[1] A. Burns (1994) Preemptive Priority Based Scheduling: An Appropriate Engi-
neering Approaches, Advances in Real-Time Systems, pp225–248.

[2] CPU Scheduling in Operating System — Studytonight. (2021). Study-


tonight.com. https://www.studytonight.com/operating-system/cpu-scheduling

[3] Hamayun. M., & Khurshid. H. (2015) An Optimized Shortest Job First Schedul-
ing Algorithm For CPU Scheduling, J. Appl. Environ. Biol. Sci, 5(12), 42-46.

[4] IncludeHelp’s Author. (6 March, 2015). IncludeHelp. Re-


trieved from STRF: Shortest Remaining Time First Scheduling:
https://www.includehelp.com/operating-systems/srtf-shortest-remaining-
time-first-scheduling-algorithm.aspx: :text=SRTF

[5] Jayant Kaushik,StudyTonight Author (2013). Shortest Remaining Time


First Scheduling Algorithm. Retrieved from StudyTonight Web site:
https://www.studytonight.com/operating-system/shortest-remaining-time-
first-scheduling-algorithm

[6] Kishor, L., Goyal, D., Singh, R., Sharma, P. (2011). Optimized scheduling
algorithm. In Proceedings of the IJCA International Conference on Computer
Communication and Networks (CSICOMNET’11) (pp. 130-134).

[7] Singhal, T. (7 November, 2019). GeeksforGeeks. Retrieved from In-


troduction of Shortest Remaining Time First (SRTF) algorithm:
https://www.geeksforgeeks.org/introduction-of-shortest-remaining-time-first-
srtf-algorithm/

18

You might also like