Skip to content

Latest commit

Β 

History

History
258 lines (210 loc) Β· 7.46 KB

cpu-scheduling.md

File metadata and controls

258 lines (210 loc) Β· 7.46 KB

CPU μŠ€μΌ€μ€„λ§

1. μ£Όμš” CPU μŠ€μΌ€μ€„λ§ μ•Œκ³ λ¦¬μ¦˜

1.1 비선점 μŠ€μΌ€μ€„λ§

// 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);
}

1.2 선점 μŠ€μΌ€μ€„λ§

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()
                );
            }
        }
    }
}

2. μŠ€μΌ€μ€„λ§ μ„±λŠ₯ λ©”νŠΈλ¦­

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;
    }
}

3. κ³ κΈ‰ μŠ€μΌ€μ€„λ§ 기법

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 μŠ€μΌ€μ€„λ§ λ©”μ»€λ‹ˆμ¦˜μ„ 톡해:

  1. μŠ€μΌ€μ€„λ§ μ•Œκ³ λ¦¬μ¦˜

    • 비선점 μŠ€μΌ€μ€„λ§ (FCFS, SJF)
    • 선점 μŠ€μΌ€μ€„λ§ (Round Robin, Priority)
    • ν˜Όν•© μŠ€μΌ€μ€„λ§ (닀단계 큐)
  2. μ„±λŠ₯ 평가

    • 평균 λŒ€κΈ° μ‹œκ°„
    • CPU ν™œμš©λ„
    • μ²˜λ¦¬λŸ‰
    • λ°˜ν™˜ μ‹œκ°„
  3. κ³ κΈ‰ 기법

    • μ‹€μ‹œκ°„ μŠ€μΌ€μ€„λ§
    • 곡정 μŠ€μΌ€μ€„λ§
    • μš°μ„ μˆœμœ„ 상속

을 κ΅¬ν˜„ν•˜μ—¬ 효율적인 ν”„λ‘œμ„ΈμŠ€ 관리λ₯Ό ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

λ©΄μ ‘μ—μ„œ μ€‘μš”ν•œ 포인트:

  1. 각 μ•Œκ³ λ¦¬μ¦˜μ˜ μž₯단점
  2. μ ν•©ν•œ μ‚¬μš© 사둀
  3. μ„±λŠ₯ νŠΈλ ˆμ΄λ“œμ˜€ν”„
  4. μ‹€μ œ μ‹œμŠ€ν…œμ—μ„œμ˜ κ΅¬ν˜„ 방식