03_Scheduling

Download Report

Transcript 03_Scheduling

Scheduling
http://is.konkuk.ac.kr
서울 광진구 화양동 건국대학교 정보통신대학 컴퓨터 공학부 IS 연구실 (새천년관 1008호 )
Process Manager Overview
Program
Process
Abstract Computing Environment
File
Management
Deadlock
Process
Description
Protection
Device
Manager
Memory
Manager
Synchronization
Resource
Management
Scheduler
Device
Memory
CPU
Other H/W
2
Process Scheduling
제한된 자원의 효율적 사용
CPU 사용 경쟁을 해결하기 위한 정책
CPU를 사용하려는 데이터들은 레디 리스트(혹은 큐)에 있다
시스템 성능에 큰 영향을 미침
Scheduler
Process(레디 리스트(큐)에 있는 수행될 쓰레드나 프로세스)를 선정
Dispatcher
프로세스 스케줄러에 의해 선정된 프로세스에게
CPU를 할당하는 루틴
Running Process를 yield 하여 ready list에 삽입
Context Switch
(새로운 프로세스에게 CPU를 쓰게 하고, 기존의 프로세스는 레디 리스트(큐)로 이동)
기존에 사용한 프로세스에 대한 CPU의 상태, 프로그램과 데이터의 위치를 PCB에 저장하였다가 나중
에 다시 CPU가 할당되었을 때 불러와서 사용하도록 하는 기능.
3
The Scheduler
States
Process
Descriptor
Ready Process
Enqueue
Ready List
Dispatch
Scheduler
Context Switch
CPU
4
Context switch
Process 1
EXE
Interrupt
Or
System Call
OS
Process 2
Process state
 PCB[1]
Ready
PCB[2]
 Process state
EXE
Ready / Wait
EXE
Process state
 PCB[2]
PCB[1]
 Process state
Interrupt
Or
System Call
Ready / Wait
5
Process Execution
비자발적으로 CPU를 양보(타임쉐어링 등)
Preemption or Voluntary yield
자발적으로 CPU를 양보
New
Process
Ready List
Allocate
Scheduler
Resource
Manager
CPU
Done
Request
Resources
6
고려사항
Utilization (CPU 이용률을 최대로 하자)
Throughput (단위시간당 처리율을 극대화 하자)
Turnaround time (CPU에 들어와서 연산이 끝난 후 반환되기까지의 시간)
Waiting time (레디 리스트에서 스케줄러를 거쳐 CPU까지 가기 위한 대기시간)
Response time (상호 모드에서 IO에 관한 응답시간)
예를 들어 입력을 했을 때 그에 따른 CPU의 결과(?)물이 보여지기까지의 시간
Utilization, Throughput 최대화
Turnaround time, Waiting time, Response time 최소화
이용률과 처리율은 극대화하면서 시간은 극소화 시키는게 중요하다.
7
Scheduling Strategy
Nonpreemptive Strategies (Voluntary Sharing)
Process 의 시작에서 끝까지 CPU 점유
First-Come-First-Served
Shortest Job First
Priority Scheduling
Deadline Scheduling
위의 것들은 시작은 Nonpreemptive Strategies이지만 나중에
Preemptive Strategies로 바뀌기도 한다.
물론 여기 나열된 것들만 존재 하는것은 아니다.
Preemptive Strategies (Involuntary Sharing)
외부 요청(인터럽트 등)에 의해 CPU 를 넘겨줌
Round Robin
Multiple-level Queues
반드시 Preemptive Strategies이여야만 한다.
여기 나온것만 나오지는 않는다. 이 외에도 많이 존재한다.
8
yield Operation(1)
자발적으로 CPU 사용권을 넘길 때 부름
구현 예:
yield(r, s) {
memory[r] = PC; // PC : Program Counter
PC = memory[s];
}
호출 방법
yield(PC가 보존될 자신의 영역, 상대방 Process)
yield(p1, p2); 로 표현
9
yield Operation(2)
Voluntary Scheduling
p1
p2
….
yield(*, scheduler);
…
….
....
…
자발적인 scheduler 호출
scheduler
….
s = select(…);
yield(*, s);
…
yield operation에 의해
p2 process/thread 진행
Involuntary Scheduling
p1
….
....
…
Interrupt & System Call
에 의한 비자발적인 Scheduler
호출
scheduler
….
s = select(…);
yield(*, s);
…
p2
….
....
…
yield operation에 의해
p2 process/thread 진행
10
First-Come-First-Served
도착 순서대로 CPU 할당
Gantt chart
Process
Arrival Service
Time
Time
P1
P2
0
P1
0
13
P2
1
2
P3
2
7
0
P4
3
5
P1
도착
P5
4
3
13
P3
P4
15
22
P5
27
30
(Service time 과 작업순서 )
1
2
3
P2
도착
P3
도착
P4
도착
4
5
P5 …
도착
(시간대별 Process 도착 순서)
평균 반환시간 =
((13  0)  (15  1)  (22  2)  (27  3)  (30  4))
5
= 19ms
평균 대기시간 =
(0  (13  1)  (15  2)  (22  3)  (27  4))
5
= 13ms
Turn around tile = CPU time + waiting time
11
Shortest Job First
CPU Burst 시간이 가장 짧은 Process가 먼저 할당
즉 CPU를 사용하는 시간이 가장 짧은 프로세스부터 먼저 할당
Arrival time을 고려하지 않음
 해결 : Shortest remained time first
Process
Service
Time
P1
13
P2
2
P3
7
P4
3
P5
5
Gantt chart
P2 P4
P5
0
2
5
P3
10
P1
17
30
(Service time 과 작업순서 )
(2  5  10  17  30)
평균 반환시간 =
5
= 12.8ms
(0  2  5  10  17 )
5
= 6.8ms
평균 대기시간 =
12
Priority Scheduling
각 Process에게 Priority가 주어지고, Priority가 높은
Process가 CPU에 먼저 할당
(priority)
Proces
s
Arrival
Time
Service
Time
P1
3
13
P2
4
2
P3
1
7
P4
2
3
P5
3
5
3
4
P1
1
0
P2
2
P3
2
P3
3
P1
10
3
P4
3
P4
7
1
4
P5
23
(Service time 과 작업순서 )
평균 반환시간 =
P5
P2
28
30
(7  10  23  28  30)
= 19.6ms
5
P6(P1이 1초 정도 실행되는
도중에 침입)가 추가되면
(0  7  10  23  28)
평균 대기시간 =
어떻게 바뀔까?
5
Non-preemptive이면? 15.7
Preemptive이면? 14.8
= 13.6ms
13
Deadline Scheduling
각 Process 마다 최고 Deadline이 정해져 있어 그 시간
안에 작업을 끝내야 함.
여러 가지 방법으로 Scheduling 될 수 있음.
Case 1
P5
Process
Deadline
Service
Time
P1
29
13
P2
20
2
P3
27
7
P4
None
3
P5
10
5
0
P2
5
P3
7
P1
P4
14
27
30
Case 2
P2
0
P5
P3
2
7
P1
P4
14
27
30
Case 3
P5
0
P2
5
P1
7
P3
20
P4
27
30
14
Preemptive Schedulers
레디리스트에 새로운 작업이 들어오면 작동
타임슬라이스가 호출?
Preemption or Voluntary yield
스케줄러는 언제, 왜 호출되는가?
New
Process
Ready
List
Scheduler
CPU
Done
Highest priority Process is guaranteed to be running at all
times
Or at least at the beginning of a time slice
Dominant form of contemporary scheduling
But complex to build & analyze
인터럽트가 발생하면 실행중인 프로세스가
CPU를 다른 곳에 빼앗기는 경우가 생긴다.
인터럽트는 외부적 요소이므로 프로세스로
인한 CPU점유보다 우선시된다.
A. Round Robin
B. Multiple-leve Queues
15
Round Robin (1)
미리 정의된 시간 할당량과 순환 큐의 구조로 설계
CPU Scheduler가 시간 할당 단위만큼 각각의 Process
를 실행
시간 할당량을 4로 가정
(도착순서)
Process
Arrival Service
Time
Time
P1
0
13
P2
1
2
P3
2
7
P4
3
3
P5
4
5
P1
P2
P5
P1
0
P4
P2
4
P3
P3
6
P3
P4
10
P2
P5
13
P4
P1
P1
17
P5
P3 P5
21 24
25
P1
29
(Service time 과 작업순서 )
16
Round Robin (2)
P1
0
P2
4
P3
6
P4
10
13
P5
P1
17
P3 P5
21
24
25
P1
29
( Service time 과 작업순서 )
평균 반환시간 =
(( 30  0)  (6  1)  (24  2)  (13  3)  (25  4))
5
= 17.6ms
평균 대기시간 =
(((17  4)  (25  21))  (4  1)  (( 6  2)  (21  10))  (10  3)  ((13  4)  (24  17 )))
5
= 11.6ms
17
Multi-Level Queues
Foreground : high priority
Background : low priority
(급하게 실시간으로 처리하지 않아도 될 일을 처리할 때 background작업을 한다.)
각 level 에서는 RR(각각의 scheduler) 사용
BSD Unix scheduling : 32 run queues
0~7 for System
8~31 for user
Preemption or voluntary yield
Ready List0
New
Process
Ready List1
Ready List2
Scheduler
CPU
Done
...
Ready Listn
멀티레벨 피드백 : 오랫동안 하위레벨에 있던 프로세스를 상위레벨로 상승시키는 것
라이징 : 하위레벨의 프로세스를 상위단계로 올려주는 것
18
Linux Scheduler (1)
개요
Linux의 Kernel 내부에 위치
Ready 상태의 User Process들을 Scheduling
구성요소
Scheduling Policy
해당 Process의 정책을 표시 (FIFO, RR, OTHER)
Scheduling Priority
해당 Process의 우선순위를 표시
RR ::= Round Robin -_-;; 왜 몰랐지..;;
19
Linux Scheduler (2)
Linux Scheduler가 사용하는 Policy
(1순위) SCHED_FIFO (Static Priority)
FCFS (First-Come First-Serve) 정책에 의해
실행이 종료될 때까지 실행
더 높은 우선순위의 Process 발견 시 선점됨
(2순위) SCHED_RR (Static Priority)
같은 Priority의 Process들 끼리
Round-robin 정책에 의거 CPU 자원을 공유하며 실행
더 높은 우선순위의 Process 발견 시 선점됨
(3순위) SCHED_OTHER (Dynamic Priority)
-(1순위와 2순위가 비었을 경우에만 작동한다.)
SCHED_FIFO, SCHED_RR Process들이 모두 휴면상태(Blocked
등)일 때만 실행
Process자체의 우선순위가 매 실행 시 마다 변동
 Starvation을 막기 위함
일반적인 Linux Process들이 사용
20
Linux Scheduler (3)
Linux Scheduler의 우선순위
Process 생성시
SCHED_OTHER를 사용하도록 설정
set_scheduler() API를 사용, SCHED_FIFO, SCHED_RR로 변경
SCHED_FIFO, SCHED_RR
명시적으로 우선순위를 바꾸지 않는 한, Process 종료 시까지 고정
1 ~ 99까지의 우선순위 (Static Priority)
SCHED_OTHER
Process 실행 시마다, Scheduler가 임의로 변경
0 ~ 999까지의 우선순위 (Dynamic Priority)
특정 Process가 오래 실행될 수록  우선순위가 떨어짐
특정 프로세스가 오래 실행될수록 우선순위가 왜 떨어지는가?
스케줄링 정책이 잘못 되었기에 기아문제(?)가 발생한다.
21
Linux Scheduler (4)
(참고) Linux의 Time Quantum
기본값 : 10msec (1/100sec)
최초 Booting시 Kernel Parameter 전달 방식으로 변경 가능
(100msec ~ 100usec)
Time Slot
Scheduler가 SCHED_OTHER정책 사용시 사용
Time Slot = Time Quantum * 20
매 Time Slot마다 Scheduler는 SCHED_OTHER정책을 사용하는
Process들의 우선순위를 재계산
Time Slot가 너무 빈번하면 스위칭이 많이 일어나서 지연시간이
길어진다. 때문에 이를 해결하기 위해서 결국 쓰레드 방식이 많이
쓰이게 된다.
22