Real-Time Computing and Communications Lab.Hanyang University
Introduction to CPU Scheduling
Minsoo Ryu
Department of Computer Science and EngineeringHanyang University
2
2
Real-Time Computing and Communications Lab.Hanyang University
Process Scheduling1 XPage
Priority-Based Scheduling2 XPage
Proportional Share Scheduling3 XPage
Q & A4 XPage
3
3
Real-Time Computing and Communications Lab.Hanyang University
Process States
Three key states running: Instructions are being executed. waiting: The process is waiting for some event to occur
(I/O completion, signal reception). ready: The process is waiting to be assigned to a process.
4
4
Real-Time Computing and Communications Lab.Hanyang University
State Transitions
5
5
Real-Time Computing and Communications Lab.Hanyang University
Process Scheduling
Selects from among ready processes And allocates the CPU to one of them
Scheduling decisions may take place when a process: Switches from running to waiting state (e.g., I/O request) Switches from running to ready state (e.g., interrupt) Switches from waiting to ready (e.g., completion of I/O) Terminates
Scheduling only under 1 and 4 is nonpreemptive Otherwise, it is preemptive
6
6
Real-Time Computing and Communications Lab.Hanyang University
Scheduling Criteria
Performance objectives Maximize CPU utilization or throughput Minimize completion time, waiting time, or response time
Real-time constraints Satisfy deadlines
Proportional share constraints Provide CPU cycles proportional to weights
7
7
Real-Time Computing and Communications Lab.Hanyang University
Classification of Scheduling Policies
Scheduling mechanisms Priority-based scheduling Proportional share scheduling
Target processors Uniprocessor scheduling Multiprocessor scheduling
Real-Time Computing and Communications Lab.Hanyang University
Priority-Based Scheduling
9
9
Real-Time Computing and Communications Lab.Hanyang University
Priority-Based Scheduling
A priority number (integer) is associated with each process
The CPU is allocated to the process with the highest priority (smallest integer highest priority) Preemptive Nonpreemptive
Task A (priority 1)Task B (priority 2)Task C (priority 3)Task D (priority 4) time
10
10
Real-Time Computing and Communications Lab.Hanyang University
Priority-Based Scheduling Policies
Non-real-time policies FCFS (First-Come, First-Served) SJF (Shortest-Job-First) SRTF (Shortest-Remaining-Time-First)
Real-time policies RM (Rate Monotonic) EDF (Earliest Deadline First)
11
11
Real-Time Computing and Communications Lab.Hanyang University
FCFS (First-Come, First-Served)
Process Execution TimeP1 24P2 3P3 3
Suppose that processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:
Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17
P1 P2 P3
24 27 300
12
12
Real-Time Computing and Communications Lab.Hanyang University
SJF (Shortest-Job-First)
Process Arrival Time Execution TimeP1 0.0 7P2 2.0 4P3 4.0 1P4 5.0 4
Average waiting time = (0 + 6 + 3 + 7)/4 = 4
P1 P3 P2
73 160
P4
8 12
13
13
Real-Time Computing and Communications Lab.Hanyang University
SRTF (Shortest-Remaining-Time-First)
Process Arrival Time Execution TimeP1 0.0 7P2 2.0 4P3 4.0 1P4 5.0 4
Average waiting time = (9 + 1 + 0 +2)/4 = 3 SRTF is optimal in that it achieves minimum average
waiting time for a given set of processes
P1 P3P2
42 110
P4
5 7
P2 P1
16
14
14
Real-Time Computing and Communications Lab.Hanyang University
Prediction of Execution Times
15
15
Real-Time Computing and Communications Lab.Hanyang University
Prediction of Execution Times
2
4
6
8
10
12
i
it
CPU burst ( )it
“guess” ( )i6 4 6 4 13 13 13 …
10 8 6 6 5 9 11 12 …
16
16
Real-Time Computing and Communications Lab.Hanyang University
RM (Rate Monotonic)
Assumptions Processes have periods, worst-case execution times
(WCETs), and deadlines
Scheduling policy Give higher priorities to tasks with shorter periods Preemptive static priority scheduling
Optimality If a feasible static priority assignment exists for some
process set, the RM priority assignment is feasible for that process set
17
17
Real-Time Computing and Communications Lab.Hanyang University
RM (Rate Monotonic)
Consider the following tasks Process X : period = 20, WCET = 10, deadline = 20 Process Y : period = 30, WCET = 8, deadline = 30 Process Z : period = 40, WCET = 4, deadline = 40
Schedulability test CPU utilization: A set of m processes is schedulable if
• For large m,
10 20 30 40 50 60 70 80 900
1/(2 1)mU m 1
/m
i ii
U e p
1/(2 1) ln 2 0.69mm
18
18
Real-Time Computing and Communications Lab.Hanyang University
EDF (Earliest Deadline First)
Scheduling policy Give higher priorities to tasks with earlier absolute
deadlines Preemptive dynamic priority scheduling
Optimality If a feasible dynamic priority assignment exists for some
process set, the EDF priority assignment is feasible for that process set
Schedulability test A set of m processes is schedulable if and only if 1U
19
19
Real-Time Computing and Communications Lab.Hanyang University
EDF (Earliest Deadline First)
Consider the following tasks Process X : period = 20, WCET = 10, deadline = 20 Process Y : period = 30, WCET = 10, deadline = 30 Process Z : period = 40, WCET = 5, deadline = 40
10 20 30 40 50 60 70 80 900
10 20 30 40 50 60 70 80 900
deadline miss of Task Z
EDF schedule
RM schedule
20
20
Real-Time Computing and Communications Lab.Hanyang University
Non-schedulable Behavior
Consider the following tasks Process X : period = 20, WCET = 10, deadline = 20 Process Y : period = 30, WCET = 8, deadline = 30 Process Z : period = 40, WCET = 15, deadline = 40
RM schedule10 20 30 40 50 60 70 80 900
10 8 10 8 10 10 108
deadlinemiss
deadlinemiss
10 20 30 40 50 60 70 80 900
deadlinemiss
EDF schedule
10 8 15 10 8 10 15 10 8
deadlinemiss
deadlinemiss
deadlinemiss
Real-Time Computing and Communications Lab.Hanyang University
Proportional Share Scheduling
22
22
Real-Time Computing and Communications Lab.Hanyang University
Proportional Share Scheduling
Basic concept A weight value is associated with each process The CPU is allocated to the process in proportion to its
weight
Two contexts Fair queueing (in the context of communication networks)
• Packet scheduling Proportional share (in the context of operating systems)
• Process scheduling
time
Task A (weight 25.0%)
Task B (weight 12.5%)Task C (weight 50.0%)Task D (weight 12.5%)
23
23
Real-Time Computing and Communications Lab.Hanyang University
Scheduling Algorithms
Network scheduling PGPS (= WFQ), Demers et al., 1989 Virtual Clock, Lixia Zhang, 1990 SCFQ, Golestani, 1994 SFQ, Goyal et al., 1996 WF2Q, Bennett et al., 1996
CPU scheduling Lottery and Stride, Waldspurger, 1995 Hierarchical SFQ, Goyal et al., 1996 BVT, Duda et al., 1999 VTRR, Nieh et al., 2001
24
24
Real-Time Computing and Communications Lab.Hanyang University
GPS (Generalized Processor Sharing) A GPS server is defined by (Kleinrock, 1976)
: the amount of session traffic served in an interval
: weight of session
Packets of all sessions are served simultaneously Idealized fluid-flow system or bit-by-bit weighted round-
robin
),( 21 ttWi ],( 21 tt
Njrr
ttWttW
j
i
j
i ...,,2,1,),(),(
21
21
ir i
i
25
25
Real-Time Computing and Communications Lab.Hanyang University
PGPS (Packet-by-Packet GPS)
PGPS is an approximation to GPS Fair queueing on a packet-by-packet basis Also known as WFQ (Weighted Fair Queueing)
Algorithm : the time at which packet will depart (finish
service) under GPS Serve packets in increasing order of
pF p
pF
GPS PGPS
26
26
Real-Time Computing and Communications Lab.Hanyang University
Fairness Bounds of PGPS
Bound on lag
• : the time at which packet departs under PGPS • : the time at which packet will depart under GPS • : the maximum packet length• If and , then the lag is 1 ms
Bound on difference of services received
rLFF pp
maxˆ
pF̂pF
Gbpsr 1 KbL 1max
max),0(ˆ),0( LWW ii
pp
maxL
27
27
Real-Time Computing and Communications Lab.Hanyang University
Worst-case Fair WFQ
WF2Q only considers Packets that have started receiving service under GPS
GPS(fluid-flow)
WFQ
WF2Q
WFQ satisfies only
notmax2121 ),(ˆ),( LttWttW pp
max2121 |),(ˆ),(| LttWttW pp
28
28
Real-Time Computing and Communications Lab.Hanyang University
Lottery and Stride Scheduling
Tickets: relative resource rights Task has tickets A total of tickets
Lottery scheduling: probabilistic algorithm Use random number generator to select a winning ticket Task is probabilistically guaranteed a rate of
Stride scheduling: deterministic algorithm “Stride” is inversely proportional to tickets Task with minimum “pass” value is selected and its pass
is advanced by its stride
imiM
i /ip m M
29
29
Real-Time Computing and Communications Lab.Hanyang University
Stride Scheduling
Task : tickets = 3, stride = 2 Task : tickets = 2, stride = 3 Task : tickets = 1, stride = 6
1
2
3
1pass
time 0 1 2 3 4 5 6 7 8 9 10 11 12 13
2
3
6 6
3
4 4
6
6
6
6
6
8
6
6
8
9
6
8
9
12
10
9
12
10
12
12
12
12
12
14
12
12
14
15
12
14
15
18
16
15
18
14
16
18
18
‘s pass value
2 ‘s pass value
3 ‘s pass value
Ties are broken arbitrarily1. Smallest value is chosen2. Pass is advanced by stride 3
30
30
Real-Time Computing and Communications Lab.Hanyang University