Shortest Remaining Time First (SRTF) Scheduling Algorithm
Shortest Remaining Time First (SRTF) Scheduling Algorithm
CSF3305
Operating System
Contents
1 Introduction 2
3 Source Code 4
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.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;
ShortestRemaindingTimeFirst Class
package SRTF;
import java.util.*;
public class ShortestRemaindingTimeFirst {
public ShortestRemaindingTimeFirst() {
boolean isInteger = false;
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();
}
5
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm
}
} while (!isInteger);
System.out.println("\n\nProcess Execution Sequence:");
this.setShortestProcess(-1);
6
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm
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
this.calculateAverageTurnAround(this.getTotalTurnAround());
this.calculateAverageWaiting(this.getTotalWaiting());
8
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm
return processNumber;
}
9
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm
Process Class
package SRTF;
public Process(){}
10
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm
this.setRemainingTime(BurstTime);
}
11
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm
NegativeValueException Class
package SRTF;
12
Group 17: Shortest Remaining Time First (SRTF) Scheduling Algorithm
ZeroException Class
package SRTF;
13
Group 17: Shortest Remaining Time First (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.
[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.
[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).
18