mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-10 21:43:15 +08:00
Add SRTF Algorithm (#5011)
This commit is contained in:
@ -10,3 +10,4 @@ tasks:
|
||||
vscode:
|
||||
extensions:
|
||||
- xaver.clang-format
|
||||
|
||||
|
@ -0,0 +1,69 @@
|
||||
package com.thealgorithms.scheduling;
|
||||
|
||||
import com.thealgorithms.devutils.entities.ProcessDetails;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Implementation of Shortest Remaining Time First Scheduling Algorithm.
|
||||
* In the SRTF scheduling algorithm, the process with the smallest amount of time remaining until completion is selected to execute.
|
||||
* Example:
|
||||
* Consider the processes p1, p2 and the following table with info about their arrival and burst time:
|
||||
* Process | Burst Time | Arrival Time
|
||||
* P1 | 6 ms | 0 ms
|
||||
* P2 | 2 ms | 1 ms
|
||||
* In this example, P1 will be executed at time = 0 until time = 1 when P2 arrives. At time = 2, P2 will be executed until time = 4. At time 4, P2 is done, and P1 is executed again to be done.
|
||||
* That's a simple example of how the algorithm works.
|
||||
* More information you can find here -> https://en.wikipedia.org/wiki/Shortest_remaining_time
|
||||
*/
|
||||
public class SRTFScheduling {
|
||||
protected List<ProcessDetails> processes;
|
||||
protected List<String> ready;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
* @param processes ArrayList of ProcessDetails given as input
|
||||
*/
|
||||
public SRTFScheduling(ArrayList<ProcessDetails> processes) {
|
||||
this.processes = new ArrayList<>();
|
||||
ready = new ArrayList<>();
|
||||
this.processes = processes;
|
||||
}
|
||||
|
||||
public void evaluateScheduling() {
|
||||
int time = 0, cr = 0; // cr=current running process, time= units of time
|
||||
int n = processes.size();
|
||||
int[] remainingTime = new int[n];
|
||||
|
||||
/* calculating remaining time of every process and total units of time */
|
||||
for (int i = 0; i < n; i++) {
|
||||
remainingTime[i] = processes.get(i).getBurstTime();
|
||||
time += processes.get(i).getBurstTime();
|
||||
}
|
||||
|
||||
/* if the first process doesn't arrive at 0, we have more units of time */
|
||||
if (processes.get(0).getArrivalTime() != 0) {
|
||||
time += processes.get(0).getArrivalTime();
|
||||
}
|
||||
|
||||
/* printing id of the process which is executed at every unit of time */
|
||||
// if the first process doesn't arrive at 0, we print only \n until it arrives
|
||||
if (processes.get(0).getArrivalTime() != 0) {
|
||||
for (int i = 0; i < processes.get(0).getArrivalTime(); i++) {
|
||||
ready.add(null);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = processes.get(0).getArrivalTime(); i < time; i++) {
|
||||
/* checking if there's a process with remaining time less than current running process.
|
||||
If we find it, then it executes. */
|
||||
for (int j = 0; j < n; j++) {
|
||||
if (processes.get(j).getArrivalTime() <= i && (remainingTime[j] < remainingTime[cr] && remainingTime[j] > 0 || remainingTime[cr] == 0)) {
|
||||
cr = j;
|
||||
}
|
||||
}
|
||||
ready.add(processes.get(cr).getProcessId());
|
||||
remainingTime[cr]--;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.thealgorithms.scheduling;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.thealgorithms.devutils.entities.ProcessDetails;
|
||||
import java.util.ArrayList;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SRTFSchedulingTest {
|
||||
ArrayList<ProcessDetails> processes;
|
||||
|
||||
public void initialization() {
|
||||
processes = new ArrayList<>();
|
||||
processes.add(new ProcessDetails("4", 0, 3));
|
||||
processes.add(new ProcessDetails("3", 1, 8));
|
||||
processes.add(new ProcessDetails("1", 2, 6));
|
||||
processes.add(new ProcessDetails("5", 4, 4));
|
||||
processes.add(new ProcessDetails("2", 5, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Constructor() {
|
||||
initialization();
|
||||
SRTFScheduling s = new SRTFScheduling(processes);
|
||||
assertEquals(3, s.processes.get(0).getBurstTime());
|
||||
assertEquals(8, s.processes.get(1).getBurstTime());
|
||||
assertEquals(6, s.processes.get(2).getBurstTime());
|
||||
assertEquals(4, s.processes.get(3).getBurstTime());
|
||||
assertEquals(2, s.processes.get(4).getBurstTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
void evaluateScheduling() {
|
||||
initialization();
|
||||
SRTFScheduling s = new SRTFScheduling(processes);
|
||||
s.evaluateScheduling();
|
||||
assertEquals("4", s.ready.get(0));
|
||||
assertEquals("4", s.ready.get(1));
|
||||
assertEquals("4", s.ready.get(2));
|
||||
assertEquals("1", s.ready.get(3));
|
||||
assertEquals("5", s.ready.get(4));
|
||||
assertEquals("2", s.ready.get(5));
|
||||
assertEquals("2", s.ready.get(6));
|
||||
assertEquals("5", s.ready.get(7));
|
||||
assertEquals("5", s.ready.get(8));
|
||||
assertEquals("5", s.ready.get(9));
|
||||
assertEquals("1", s.ready.get(10));
|
||||
assertEquals("1", s.ready.get(11));
|
||||
assertEquals("1", s.ready.get(12));
|
||||
assertEquals("1", s.ready.get(13));
|
||||
assertEquals("1", s.ready.get(14));
|
||||
assertEquals("3", s.ready.get(15));
|
||||
assertEquals("3", s.ready.get(16));
|
||||
assertEquals("3", s.ready.get(17));
|
||||
assertEquals("3", s.ready.get(18));
|
||||
assertEquals("3", s.ready.get(19));
|
||||
assertEquals("3", s.ready.get(20));
|
||||
assertEquals("3", s.ready.get(21));
|
||||
assertEquals("3", s.ready.get(22));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user