-
Notifications
You must be signed in to change notification settings - Fork 19.6k
/
Copy pathEDFScheduling.java
99 lines (80 loc) · 2.98 KB
/
EDFScheduling.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.thealgorithms.scheduling;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* The Earliest Deadline First (EDF) Scheduling class implements a dynamic scheduling algorithm.
* It assigns the CPU to processes with the earliest deadlines, ensuring that deadlines are met if possible.
* This scheduling algorithm is ideal for real-time systems where meeting deadlines is critical.
*/
public final class EDFScheduling {
private EDFScheduling() {
}
private List<Process> processes;
/**
* Constructs an EDFScheduling object with a list of processes.
*
* @param processes List of processes to be scheduled.
*/
public EDFScheduling(final List<Process> processes) {
this.processes = processes;
}
/**
* Schedules the processes using Earliest Deadline First (EDF) scheduling.
* Processes are sorted by their deadlines, and the method simulates their execution.
* It calculates the waiting time and turnaround time for each process.
*
* @return List of processes after they have been executed in order of earliest deadline first.
*/
public List<Process> scheduleProcesses() {
processes.sort(Comparator.comparingInt(Process::getDeadline));
int currentTime = 0;
List<Process> executedProcesses = new ArrayList<>();
for (Process process : processes) {
process.setWaitingTime(currentTime);
currentTime += process.getBurstTime();
process.setTurnAroundTime(process.getWaitingTime() + process.getBurstTime());
if (currentTime > process.getDeadline()) {
System.out.println("Warning: Process " + process.getProcessId() + " missed its deadline.");
}
executedProcesses.add(process);
}
return executedProcesses;
}
/**
* The Process class represents a process with an ID, burst time, deadline, waiting time, and turnaround time.
*/
public static class Process {
private String processId;
private int burstTime;
private int deadline;
private int waitingTime;
private int turnAroundTime;
public Process(String processId, int burstTime, int deadline) {
this.processId = processId;
this.burstTime = burstTime;
this.deadline = deadline;
}
public String getProcessId() {
return processId;
}
public int getBurstTime() {
return burstTime;
}
public int getDeadline() {
return deadline;
}
public int getWaitingTime() {
return waitingTime;
}
public void setWaitingTime(int waitingTime) {
this.waitingTime = waitingTime;
}
public int getTurnAroundTime() {
return turnAroundTime;
}
public void setTurnAroundTime(int turnAroundTime) {
this.turnAroundTime = turnAroundTime;
}
}
}