OS study
scheduling
- FIFO: 먼저 들어온 순서대로 큐에 있는 거 뽑아서 실행:
- SJF: 먼저 끝나는 작업 부터 실행
- STCF: 남은 실행시간이 가장 짧은 프로세스부터 먼저 처리
- RR(Round Robin): 일정 시간별로 돌아가면서 실행하는 것
STCF
job 들어오면 인터럽트 걸리고 wait 되어서 평가하고 새로운거 시작하거나 이전꺼 하거나 그래서 새로 들어오는 시점에 재평가됨
프로세스 상태 전이도 (Process State Transition)
stateDiagram-v2
new --> ready : admitted (3)
ready --> running : scheduler dispatch (0)
running --> ready : interrupt (2)
running --> blocked : I/O or event wait (1)
blocked --> ready : I/O or event completion (3)
running --> terminated : exit (4)
- (0) ready running: 스케줄러에 의해 CPU를 할당받음 (scheduler dispatch)
- (1) running blocked: I/O 요청이나 특정 이벤트 대기 (I/O or event wait)
- (2) running ready: 할당된 시간 초과 또는 다른 이유로 CPU 반납 (interrupt)
- (3) new ready: 프로세스가 생성되어 준비 큐에 진입 (admitted)
- (3) blocked ready: 대기 중이던 I/O나 이벤트가 완료되어 다시 준비 상태로 복귀 (I/O or event completion)
- (4) running terminated: 프로세스 실행이 완료되어 종료됨 (exit)
Priority Scheduling
MLFQ(Multi-Level Feedback Queue)는 여러 개의 큐를 사용하고, 각 큐마다 다른 우선순위와 time slice를 가진다.
interactive 한 job은 time slice를 짧게 해서 빠르게 응답하고, batch 한 job은 time slice를 길게 해서 효율적으로 처리할 것이다.
그래서 cpu를 조금 쓰고 I/O를 많이 쓰는 job은 우선순위 올려주고, cpu를 많이 쓰는 job은 우선순위 내린다.
RULE:
- If priority(A) > priority(B), run A (even if B is already running)
- If priority(A) = priority(B), run A (round-robin)
- When a job enters the system, it enters the highest priority queue
- If a job uses up an entire time slice while running, its priority is reduced (i.e., it moves down on queue). Once a job uses up its time allotment at a given level (regardless of how many times it has given up the CPU), its priority is reduced (i.e., it moves down on queue)
- If a job voluntarily gives up the CPU (e.g., by performing I/O) before the time slice expires, it stays at the same priority level After some time period S, move all the jobs in the system to the topmost queue
우선순위를 주었을 때 제일 중요한 건 기아상태에 빠지지 않게 해야함.
Priority Inversion
lock 한 상태의 우선순위 low 작업때문에 high 작업이 unlock 기다리는데 medium이 같은 자원 안쓰는 프로세스라면 high 작업이 medium 때문에 기다리는 문제 발생.
Solution
같은 자원을 쓰는 프로세스라면 우선순위를 높여서 해결.
[기본 우선순위 가정] L(2) < M(4) < H(8)
예시 1: 단일 락 (Single Lock)
L이 락 점유.M이 대기L의 우선순위를 4로 상향.H가 대기L의 우선순위를 8로 추가 상향.
예시 2: 다중 락 (Multiple Locks)
L은 락1,M은 락2 점유.M이 락1 대기L우선순위 4로 상향.H가 앞서M이 점유 중인 락2 대기M우선순위 8로 상향.- 연쇄 작용
M이 대기하는 락1의 소유자L의 우선순위도 8로 연쇄 상향됨.