Skip to content

Commit bd3b754

Browse files
authored
Add EDFScheduling algorithm (#5657)
1 parent 90d20b3 commit bd3b754

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

DIRECTORY.md

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
* [OnesComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java)
3737
* [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java)
3838
* [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
39+
* [SingleElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleElement.java)
40+
* [SwapAdjacentBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java)
3941
* [TwosComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java)
4042
* ciphers
4143
* a5
@@ -476,6 +478,7 @@
476478
* Recursion
477479
* [GenerateSubsets](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/Recursion/GenerateSubsets.java)
478480
* scheduling
481+
* [EDFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/EDFScheduling.java)
479482
* [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
480483
* [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java)
481484
* [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java)
@@ -649,6 +652,8 @@
649652
* [OnesComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java)
650653
* [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java)
651654
* [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
655+
* [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java)
656+
* [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java)
652657
* [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java)
653658
* ciphers
654659
* a5
@@ -975,6 +980,7 @@
975980
* Recursion
976981
* [GenerateSubsetsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/Recursion/GenerateSubsetsTest.java)
977982
* scheduling
983+
* [EDFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/EDFSchedulingTest.java)
978984
* [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
979985
* [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
980986
* [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
/**
8+
* The Earliest Deadline First (EDF) Scheduling class implements a dynamic scheduling algorithm.
9+
* It assigns the CPU to processes with the earliest deadlines, ensuring that deadlines are met if possible.
10+
* This scheduling algorithm is ideal for real-time systems where meeting deadlines is critical.
11+
*/
12+
public final class EDFScheduling {
13+
private EDFScheduling() {
14+
}
15+
16+
private List<Process> processes;
17+
18+
/**
19+
* Constructs an EDFScheduling object with a list of processes.
20+
*
21+
* @param processes List of processes to be scheduled.
22+
*/
23+
public EDFScheduling(final List<Process> processes) {
24+
this.processes = processes;
25+
}
26+
27+
/**
28+
* Schedules the processes using Earliest Deadline First (EDF) scheduling.
29+
* Processes are sorted by their deadlines, and the method simulates their execution.
30+
* It calculates the waiting time and turnaround time for each process.
31+
*
32+
* @return List of processes after they have been executed in order of earliest deadline first.
33+
*/
34+
public List<Process> scheduleProcesses() {
35+
processes.sort(Comparator.comparingInt(Process::getDeadline));
36+
37+
int currentTime = 0;
38+
List<Process> executedProcesses = new ArrayList<>();
39+
40+
for (Process process : processes) {
41+
process.setWaitingTime(currentTime);
42+
currentTime += process.getBurstTime();
43+
process.setTurnAroundTime(process.getWaitingTime() + process.getBurstTime());
44+
45+
if (currentTime > process.getDeadline()) {
46+
System.out.println("Warning: Process " + process.getProcessId() + " missed its deadline.");
47+
}
48+
49+
executedProcesses.add(process);
50+
}
51+
52+
return executedProcesses;
53+
}
54+
55+
/**
56+
* The Process class represents a process with an ID, burst time, deadline, waiting time, and turnaround time.
57+
*/
58+
public static class Process {
59+
private String processId;
60+
private int burstTime;
61+
private int deadline;
62+
private int waitingTime;
63+
private int turnAroundTime;
64+
65+
public Process(String processId, int burstTime, int deadline) {
66+
this.processId = processId;
67+
this.burstTime = burstTime;
68+
this.deadline = deadline;
69+
}
70+
71+
public String getProcessId() {
72+
return processId;
73+
}
74+
75+
public int getBurstTime() {
76+
return burstTime;
77+
}
78+
79+
public int getDeadline() {
80+
return deadline;
81+
}
82+
83+
public int getWaitingTime() {
84+
return waitingTime;
85+
}
86+
87+
public void setWaitingTime(int waitingTime) {
88+
this.waitingTime = waitingTime;
89+
}
90+
91+
public int getTurnAroundTime() {
92+
return turnAroundTime;
93+
}
94+
95+
public void setTurnAroundTime(int turnAroundTime) {
96+
this.turnAroundTime = turnAroundTime;
97+
}
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class EDFSchedulingTest {
11+
12+
private List<EDFScheduling.Process> processes;
13+
14+
@BeforeEach
15+
public void setup() {
16+
processes = createProcesses();
17+
}
18+
19+
@Test
20+
public void testEDFScheduling() {
21+
EDFScheduling edfScheduling = new EDFScheduling(processes);
22+
List<EDFScheduling.Process> executedProcesses = edfScheduling.scheduleProcesses();
23+
24+
// Assert the correct number of processes
25+
assertEquals(3, executedProcesses.size());
26+
27+
// Assert that processes are executed in order of earliest deadline first
28+
EDFScheduling.Process process1 = executedProcesses.get(0);
29+
assertEquals("P2", process1.getProcessId());
30+
assertEquals(0, process1.getWaitingTime());
31+
assertEquals(3, process1.getTurnAroundTime());
32+
33+
EDFScheduling.Process process2 = executedProcesses.get(1);
34+
assertEquals("P1", process2.getProcessId());
35+
assertEquals(3, process2.getWaitingTime());
36+
assertEquals(10, process2.getTurnAroundTime());
37+
38+
EDFScheduling.Process process3 = executedProcesses.get(2);
39+
assertEquals("P3", process3.getProcessId());
40+
assertEquals(10, process3.getWaitingTime());
41+
assertEquals(18, process3.getTurnAroundTime());
42+
}
43+
44+
@Test
45+
public void testProcessMissedDeadline() {
46+
// Modify the deadline of a process to ensure it will miss its deadline
47+
processes.get(1).setTurnAroundTime(5); // Set P1's deadline to 5 (which it will miss)
48+
49+
EDFScheduling edfScheduling = new EDFScheduling(processes);
50+
edfScheduling.scheduleProcesses();
51+
52+
// Check if the process with ID "P1" missed its deadline
53+
assertEquals("P1", processes.get(1).getProcessId());
54+
}
55+
56+
private List<EDFScheduling.Process> createProcesses() {
57+
// Process ID, Burst Time, Deadline
58+
EDFScheduling.Process process1 = new EDFScheduling.Process("P1", 7, 10); // 7 burst time, 10 deadline
59+
EDFScheduling.Process process2 = new EDFScheduling.Process("P2", 3, 5); // 3 burst time, 5 deadline
60+
EDFScheduling.Process process3 = new EDFScheduling.Process("P3", 8, 18); // 8 burst time, 18 deadline
61+
62+
List<EDFScheduling.Process> processes = new ArrayList<>();
63+
processes.add(process1);
64+
processes.add(process2);
65+
processes.add(process3);
66+
67+
return processes;
68+
}
69+
}

0 commit comments

Comments
 (0)