// FCFS (First-Come, First-Served)
struct Process {
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
};
void fcfs_scheduling(Process processes[], int n) {
int current_time = 0;
for (int i = 0; i < n; i++) {
// νλ‘μΈμ€κ° λμ°©νμ§ μμλ€λ©΄ λκΈ°
if (current_time < processes[i].arrival_time) {
current_time = processes[i].arrival_time;
}
processes[i].waiting_time =
current_time - processes[i].arrival_time;
current_time += processes[i].burst_time;
processes[i].turnaround_time =
current_time - processes[i].arrival_time;
}
}
// SJF (Shortest Job First)
void sjf_scheduling(Process processes[], int n) {
sort(processes, n); // burst_time κΈ°μ€μΌλ‘ μ λ ¬
fcfs_scheduling(processes, n);
}
public class RoundRobinScheduler {
private final Queue<Process> readyQueue = new LinkedList<>();
private final int timeQuantum;
public void schedule() {
while (!readyQueue.isEmpty()) {
Process currentProcess = readyQueue.poll();
// νμ νν
λ§νΌ μ€ν
int executionTime = Math.min(
timeQuantum,
currentProcess.getRemainingTime()
);
executeProcess(currentProcess, executionTime);
// νλ‘μΈμ€κ° μλ£λμ§ μμλ€λ©΄ λ€μ νμ μ½μ
if (currentProcess.getRemainingTime() > 0) {
readyQueue.offer(currentProcess);
}
}
}
}
// Priority Scheduling
public class PriorityScheduler {
private final PriorityQueue<Process> readyQueue;
public PriorityScheduler() {
this.readyQueue = new PriorityQueue<>((p1, p2) ->
p2.getPriority() - p1.getPriority());
}
public void addProcess(Process process) {
readyQueue.offer(process);
// μ°μ μμ μμ νμ 체ν¬
checkPriorityInversion();
}
private void checkPriorityInversion() {
// μ°μ μμ μμ ꡬν
Process highPriorityProcess = readyQueue.peek();
if (highPriorityProcess != null &&
highPriorityProcess.isBlocked()) {
Process blockingProcess =
highPriorityProcess.getBlockingProcess();
if (blockingProcess != null) {
blockingProcess.setTemporaryPriority(
highPriorityProcess.getPriority()
);
}
}
}
}
public class SchedulingMetrics {
// 1. νκ· λκΈ° μκ° κ³μ°
public double calculateAverageWaitingTime(List<Process> processes) {
return processes.stream()
.mapToDouble(Process::getWaitingTime)
.average()
.orElse(0.0);
}
// 2. νκ· λ°ν μκ° κ³μ°
public double calculateAverageTurnaroundTime(
List<Process> processes) {
return processes.stream()
.mapToDouble(p ->
p.getCompletionTime() - p.getArrivalTime())
.average()
.orElse(0.0);
}
// 3. CPU μ¬μ©λ₯ κ³μ°
public double calculateCpuUtilization(
List<Process> processes,
double totalTime) {
double busyTime = processes.stream()
.mapToDouble(Process::getBurstTime)
.sum();
return (busyTime / totalTime) * 100;
}
// 4. μ²λ¦¬λ κ³μ°
public double calculateThroughput(
List<Process> processes,
double totalTime) {
return processes.size() / totalTime;
}
}
public class AdvancedScheduler {
// 1. λ€λ¨κ³ ν μ€μΌμ€λ§
public class MultilevelQueueScheduler {
private final List<Queue<Process>> queues;
private final List<Integer> timeQuantums;
public void schedule() {
for (int i = 0; i < queues.size(); i++) {
Queue<Process> currentQueue = queues.get(i);
int timeQuantum = timeQuantums.get(i);
// μμ νκ° λΉμ΄μμ λλ§ νμ ν μ²λ¦¬
if (!currentQueue.isEmpty()) {
Process process = currentQueue.poll();
executeProcess(process, timeQuantum);
// νλ‘μΈμ€κ° μλ£λμ§ μμλ€λ©΄
if (process.getRemainingTime() > 0) {
// λ€μ νμ νλ‘ μ΄λ
if (i < queues.size() - 1) {
queues.get(i + 1).offer(process);
} else {
// μ΅νμ νλ©΄ κ°μ νμ λ€μ μ½μ
currentQueue.offer(process);
}
}
}
}
}
}
// 2. μ€μκ° μ€μΌμ€λ§
public class RealTimeScheduler {
private final PriorityQueue<Process> readyQueue;
public RealTimeScheduler() {
this.readyQueue = new PriorityQueue<>((p1, p2) ->
p1.getDeadline().compareTo(p2.getDeadline()));
}
public void scheduleEDF() { // Earliest Deadline First
while (!readyQueue.isEmpty()) {
Process process = readyQueue.poll();
if (canMeetDeadline(process)) {
executeProcess(process);
} else {
handleDeadlineMiss(process);
}
}
}
private boolean canMeetDeadline(Process process) {
return System.currentTimeMillis() +
process.getExecutionTime() <=
process.getDeadline().getTime();
}
}
// 3. 곡μ μ€μΌμ€λ§
public class FairScheduler {
private final Map<String, Double> shares;
private final Map<String, Double> usage;
public Process selectNextProcess(List<Process> runnable) {
String selectedGroup = runnable.stream()
.map(Process::getGroupId)
.min((g1, g2) ->
Double.compare(
usage.get(g1) / shares.get(g1),
usage.get(g2) / shares.get(g2)
))
.orElse(null);
return runnable.stream()
.filter(p -> p.getGroupId().equals(selectedGroup))
.findFirst()
.orElse(null);
}
}
}
μ΄λ¬ν CPU μ€μΌμ€λ§ λ©μ»€λμ¦μ ν΅ν΄:
-
μ€μΌμ€λ§ μκ³ λ¦¬μ¦
- λΉμ μ μ€μΌμ€λ§ (FCFS, SJF)
- μ μ μ€μΌμ€λ§ (Round Robin, Priority)
- νΌν© μ€μΌμ€λ§ (λ€λ¨κ³ ν)
-
μ±λ₯ νκ°
- νκ· λκΈ° μκ°
- CPU νμ©λ
- μ²λ¦¬λ
- λ°ν μκ°
-
κ³ κΈ κΈ°λ²
- μ€μκ° μ€μΌμ€λ§
- 곡μ μ€μΌμ€λ§
- μ°μ μμ μμ
μ ꡬννμ¬ ν¨μ¨μ μΈ νλ‘μΈμ€ κ΄λ¦¬λ₯Ό ν μ μμ΅λλ€.
λ©΄μ μμ μ€μν ν¬μΈνΈ:
- κ° μκ³ λ¦¬μ¦μ μ₯λ¨μ
- μ ν©ν μ¬μ© μ¬λ‘
- μ±λ₯ νΈλ μ΄λμ€ν
- μ€μ μμ€ν μμμ ꡬν λ°©μ