Skip to content

Commit fd84b0b

Browse files
authored
Add SRTF Algorithm (#5011)
1 parent 570f7e7 commit fd84b0b

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

Diff for: .gitpod.yml

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ tasks:
1010
vscode:
1111
extensions:
1212
- xaver.clang-format
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import com.thealgorithms.devutils.entities.ProcessDetails;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* Implementation of Shortest Remaining Time First Scheduling Algorithm.
9+
* In the SRTF scheduling algorithm, the process with the smallest amount of time remaining until completion is selected to execute.
10+
* Example:
11+
* Consider the processes p1, p2 and the following table with info about their arrival and burst time:
12+
* Process | Burst Time | Arrival Time
13+
* P1 | 6 ms | 0 ms
14+
* P2 | 2 ms | 1 ms
15+
* 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.
16+
* That's a simple example of how the algorithm works.
17+
* More information you can find here -> https://en.wikipedia.org/wiki/Shortest_remaining_time
18+
*/
19+
public class SRTFScheduling {
20+
protected List<ProcessDetails> processes;
21+
protected List<String> ready;
22+
23+
/**
24+
* Constructor
25+
* @param processes ArrayList of ProcessDetails given as input
26+
*/
27+
public SRTFScheduling(ArrayList<ProcessDetails> processes) {
28+
this.processes = new ArrayList<>();
29+
ready = new ArrayList<>();
30+
this.processes = processes;
31+
}
32+
33+
public void evaluateScheduling() {
34+
int time = 0, cr = 0; // cr=current running process, time= units of time
35+
int n = processes.size();
36+
int[] remainingTime = new int[n];
37+
38+
/* calculating remaining time of every process and total units of time */
39+
for (int i = 0; i < n; i++) {
40+
remainingTime[i] = processes.get(i).getBurstTime();
41+
time += processes.get(i).getBurstTime();
42+
}
43+
44+
/* if the first process doesn't arrive at 0, we have more units of time */
45+
if (processes.get(0).getArrivalTime() != 0) {
46+
time += processes.get(0).getArrivalTime();
47+
}
48+
49+
/* printing id of the process which is executed at every unit of time */
50+
// if the first process doesn't arrive at 0, we print only \n until it arrives
51+
if (processes.get(0).getArrivalTime() != 0) {
52+
for (int i = 0; i < processes.get(0).getArrivalTime(); i++) {
53+
ready.add(null);
54+
}
55+
}
56+
57+
for (int i = processes.get(0).getArrivalTime(); i < time; i++) {
58+
/* checking if there's a process with remaining time less than current running process.
59+
If we find it, then it executes. */
60+
for (int j = 0; j < n; j++) {
61+
if (processes.get(j).getArrivalTime() <= i && (remainingTime[j] < remainingTime[cr] && remainingTime[j] > 0 || remainingTime[cr] == 0)) {
62+
cr = j;
63+
}
64+
}
65+
ready.add(processes.get(cr).getProcessId());
66+
remainingTime[cr]--;
67+
}
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import com.thealgorithms.devutils.entities.ProcessDetails;
6+
import java.util.ArrayList;
7+
import org.junit.jupiter.api.Test;
8+
9+
class SRTFSchedulingTest {
10+
ArrayList<ProcessDetails> processes;
11+
12+
public void initialization() {
13+
processes = new ArrayList<>();
14+
processes.add(new ProcessDetails("4", 0, 3));
15+
processes.add(new ProcessDetails("3", 1, 8));
16+
processes.add(new ProcessDetails("1", 2, 6));
17+
processes.add(new ProcessDetails("5", 4, 4));
18+
processes.add(new ProcessDetails("2", 5, 2));
19+
}
20+
21+
@Test
22+
public void Constructor() {
23+
initialization();
24+
SRTFScheduling s = new SRTFScheduling(processes);
25+
assertEquals(3, s.processes.get(0).getBurstTime());
26+
assertEquals(8, s.processes.get(1).getBurstTime());
27+
assertEquals(6, s.processes.get(2).getBurstTime());
28+
assertEquals(4, s.processes.get(3).getBurstTime());
29+
assertEquals(2, s.processes.get(4).getBurstTime());
30+
}
31+
32+
@Test
33+
void evaluateScheduling() {
34+
initialization();
35+
SRTFScheduling s = new SRTFScheduling(processes);
36+
s.evaluateScheduling();
37+
assertEquals("4", s.ready.get(0));
38+
assertEquals("4", s.ready.get(1));
39+
assertEquals("4", s.ready.get(2));
40+
assertEquals("1", s.ready.get(3));
41+
assertEquals("5", s.ready.get(4));
42+
assertEquals("2", s.ready.get(5));
43+
assertEquals("2", s.ready.get(6));
44+
assertEquals("5", s.ready.get(7));
45+
assertEquals("5", s.ready.get(8));
46+
assertEquals("5", s.ready.get(9));
47+
assertEquals("1", s.ready.get(10));
48+
assertEquals("1", s.ready.get(11));
49+
assertEquals("1", s.ready.get(12));
50+
assertEquals("1", s.ready.get(13));
51+
assertEquals("1", s.ready.get(14));
52+
assertEquals("3", s.ready.get(15));
53+
assertEquals("3", s.ready.get(16));
54+
assertEquals("3", s.ready.get(17));
55+
assertEquals("3", s.ready.get(18));
56+
assertEquals("3", s.ready.get(19));
57+
assertEquals("3", s.ready.get(20));
58+
assertEquals("3", s.ready.get(21));
59+
assertEquals("3", s.ready.get(22));
60+
}
61+
}

0 commit comments

Comments
 (0)