CH 6
CH 6
Prof. Yongkun Li
中科大-计算机学院 特任教授
http://staff.ustc.edu.cn/~ykli
Ch6
Process Scheduling
1
Outline
User Space
Process Process Process
Kernel Space
Scheduling
Alg
P Process
lifecycle Scheduler
Context-
switching
2
Why scheduling is needed
• Process execution
– Consists of a cycle of CPU execution and I/O wait
– CPU burst + I/O burst
3
Why scheduling is needed
4
Topics
- Process lifecycle
- Process scheduling
- Context switching
- Scheduling criteria
- Scheduling algorithms
- Applications/Scenarios
5
Topics
- Process lifecycle
- Process scheduling
- Context switching
- Scheduling criteria
- Scheduling algorithms
- Applications/Scenarios
6
Programmer’s point of view…
• This is how a fresh programmer looks at a process’
life cycle.
(2)
Waiting
for results
Process
States
7
Kernel’s point of view…
Big Picture
New Terminated
(Just fork()-ed) (Zombie)
Ready Running
Waiting
(blocked)
Process
States
8
Kernel’s point of view…
The birth of a process.
Ready Running
Waiting
(blocked)
Process
States
9
Kernel’s point of view…
The process is ready.
It means it is ready to run but is not
running.
New A process may become Terminated
“ready” after...
- it is just created by fork();
- it has been running on the CPU
for some time and the OS chooses
Ready another Running
process to run;
- returning from blocked states.
10
Kernel’s point of view…
The processBigis Picture
running.
Ready Running
Waiting
Process
States
11
Kernel’s point of view…
The processBigis Picture
blocked.
Ready Running
Waiting
Process
States
12
Kernel’s point of view…
Example. Reading a file. Big Picture
Sometimes, the process has to wait for the response from the device and,
therefore,
New it is blocked. Terminated
Waiting
(interruptible)
Process
States
13
Kernel’s point of view…
Sometimes, a process needs to wait Big for a resource but it doesn’t want to
Picture
be disturbed while it is waiting. In other words, the process wants that
resource very much. Then, the process status is set to the uninterruptible
status. New Terminated
Ready Running
Waiting
(uninterruptible)
Process
States
14
Kernel’s point of view…
Return back to ready. Big Picture
When response arrives, the status of the process changes back to Ready.
from anyNew
one of the blocked states. Terminated
Process data
Ready Running
Waiting
Process
States
15
Kernel’s point of view…
Big Picture
The process is going to die.
New may
The process Terminated
- choose to terminate itself; or
- force to be terminated.
Ready Running
Waiting
Process
States
16
What is scheduling?
Ready Running
17
Triggering Events
• When process scheduling happens:
An existing process The CPU is freed. The scheduler should choose another process to run.
is terminated.
A process waits for The CPU is freed. The scheduler should choose another process to run.
I/O.
18
Key Issues
Ready Running
Context switching
19
Topics
- Process lifecycle
- Process scheduling
- Context switching
- Scheduling criteria
- Scheduling algorithms
- Applications/Scenarios
20
What is context switching?
• Before we can jump into the process scheduling
topic, we have to understand what “context
switching” is.
P1 P2 P3 P2
21
Switching from one process to another.
Suppose this process gives up running on the CPU,
System Memory
e.g., calling sleep(). Then:
User-space
Running Waiting
memory
Now, it is time for the scheduler to choose the next
process to run.
(1)
Kernel-space
22
Switching from one process to another.
But, before the scheduler can seize the control of the System Memory
CPU, a very important step has to be taken:
User-space
Backup all registers’ values. memory
(1)
backup
Kernel-space
23
Switching from one process to another.
Say, the scheduler decides to schedule another
System Memory
process.
(4)
Program counter
context switching
load
24
Context switching has a price to pay…
• However, context switching may be expensive…
– Even worse, the target process may be currently stored
in the hard disk.
25
Topics
- Process lifecycle
- Process scheduling
- Context switching
- Scheduling criteria
- Scheduling algorithms
- Applications/Scenarios
26
Scheduling Criteria
• How to choose which algorithm to use in a
particular situation?
Types
Algorithm Properties
Preemptive
CPU utilization Throughput
Nonpreemptive
Turnaround time Waiting time
Application Response time
Multiprocessor
Real-time sys Application requirements and algorithm
properties may vary significantly
27
Classes of process scheduling
• Non-preemptive scheduling.
When a process is chosen by the scheduler, the process would never
leave the scheduler until…
What is it?
-the process voluntarily waits for I/O, or
-the process voluntarily releases the CPU, e.g., exit().
What is the If the process is purely CPU-bound, it will seize the CPU from the time it is
catch? chosen until it terminates.
28
Classes of process scheduling
• Preemptive scheduling.
When a process is chosen by the scheduler, the process would never
leave the scheduler until…
What is it?
-the process voluntarily waits for I/O, or
-the process voluntarily releases the CPU, e.g., exit().
-particular kinds of interrupts and events are detected.
What is the If that particular event is the periodic clock interrupt, then you can have
catch? a time-sharing system.
Cons Bad for systems that emphasize the time in finishing tasks.
Where can I
Everywhere! This is the design of nowadays systems.
find it?
29
Performance measures
In algorithm design:
Turnaround
time
Throughput
Waiting
time
CPU
utilization Response
time
30
Performance measures
CPU utilization.
Throughput
Waiting
time
CPU
utilization Response
time
31
Performance measures
Throughput.
Throughput
Waiting
time
CPU
utilization Response
time
32
Performance measures
Turnaround time.
Throughput
Waiting
time
CPU
utilization Response
time
33
Performance measures
Waiting time.
Throughput
Waiting
time
CPU
utilization Response
time
34
Performance measures
Response time.
Throughput
Waiting
time
CPU
utilization Response
time
35
Challenge
Question:
Common
Can we optimize all the above
goal
measures simultaneously?
Design
Tradeoff CPU-I/O
Fairness Balance
Little conflict
36
Topics
- Process lifecycle
- Process scheduling
- Context switching
- Scheduling criteria
- Scheduling algorithms
- Applications/Scenarios
37
Scheduling algorithms
• Inputs to the algorithms.
A set of
P1 P2 P3 P4
processes
38
Scheduling algorithms
• Outputs of the algorithms.
Scheduling
Individual & average
order
waiting time
Number of context
switching
39
Different algorithms
First-come, first-served
or First-in, First-out No. Out-of-date
(FIFO)
Priority scheduling
The real implementation!
with multiple queues.
40
First-come, first-served scheduling
• Example 1.
Output
Gantt Chart No preemption
P1 P2 P3
0 2 4 6 8 1 1 1 1 1 2 2 2 2 2 3
0 2 4 6 8 0 2 4 6 8 0
Input
41
First-come, first-served scheduling
• Example 2.
Output
Gantt Chart
P3 P2 P1
0 2 4 6 8 1 1 1 1 1 2 2 2 2 2 3
0 2 4 6 8 0 2 4 6 8 0
Waiting time: P1 = 4; P2 = 2; P3 = 0;
Average waiting time = (4+2+0)/3 = 2; Task Arrival CPU
(which is 16 in the previous case) Time Req.
P3 0 3
43
Different algorithms
First-come, first-served
or First-in, First-out No. Out-of-date
(FIFO)
Priority scheduling
The real implementation!
with multiple queues.
44
Non-preemptive SJF
P1P1 P1P1
0 2 4 6 8 1 1 1 1
0 2 4 6
Time = 5
0
2
4 Not allow preemption
45
Non-preemptive SJF
P1
0 2 4 6 8 1 1 1 1
0 2 4 6
Time = 7
46
Non-preemptive SJF
P1 P3
0 2 4 6 8 1 1 1 1
0 2 4 6
Time = 7
47
Non-preemptive SJF
In this example, we use FIFO to break the tie.
P1 P3 P2
0 2 4 6 8 1 1 1 1
0 2 4 6
Time = 8
48
Non-preemptive SJF
P1 P3 P2 P4 P4
0 2 4 6 8 1 1 1 1
0 2 4 6
Time = 16
12
49
Non-preemptive SJF
P1 P3 P2 P4 P4
0 2 4 6 8 1 1 1 1
0 2 4 6
Waiting time:
P1 = 0; P2 = 6; P3 = 3; P4 = 7;
50
Preemptive SJF
0 2 4 6 8 1 1 1 1
0 2 4 6
51
Preemptive SJF
P1
0 2 4 6 8 1 1 1 1
0 2 4 6
Time = 0
52
Preemptive SJF
P1 P2
0 2 4 6 8 1 1 1 1
0 2 4 6
Time = 2 P2
Preempted!
is selected!
53
Preemptive SJF
P1 P2 P3
0 2 4 6 8 1 1 1 1
0 2 4 6
Time = 4 P3
Preempted!
is selected!
54
Preemptive SJF
P1 P2 P3 P2
0 2 4 6 8 1 1 1 1
0 2 4 6
Time = 5 P2
Preempted!
is selected!
55
Preemptive SJF
P1 P2 P3 P2 P4 P4 P1 P1
0 2 4 6 8 1 1 1 1
0 2 4 6
Time
Time==16
11
7
56
Preemptive SJF
P1 P2 P3 P2 P4 P4 P1 P1
0 2 4 6 8 1 1 1 1
0 2 4 6
Waiting time:
P1 = 9; P2 = 1; P3 = 0; P4 = 2;
57
SJF: Short summary
Predicted
value
First-come, first-served
or First-in, First-out No. Out-of-date
(FIFO)
Priority scheduling
The real implementation!
with multiple queues.
61
Round-robin
• Round-Robin (RR) scheduling is preemptive.
– Every process is given a quantum, or the amount of time
allowed to execute.
– When the quantum of a process is used up (i.e., 0), the
process releases the CPU and this is the preemption.
– Then, the scheduler steps in and it chooses the next
process which has a non-zero quantum to run.
-The process queue is sorted according the processes’ Task Arrival CPU Req.
arrival time, in an ascending order. Time Initial & Remain
(This rule allows us to break tie.) P1 0 7 7
P2 2 4 4
P3 4 1 1
P4 5 4 4
63
Round-robin
P1
0 2 4 6 8 1 1 1 1
0 2 4 6
Time = 0
64
Round-robin
P1 P2
0 2 4 6 8 1 1 1 1
0 2 4 6
P1’s quantum is 0;
Time = 2
P2 is selected!
65
Round-robin
P1 P2 P3
0 2 4 6 8 1 1 1 1
0 2 4 6
P1’s & P2’s quanta are 0;
Time = 4
P3 is selected!
66
Round-robin
P1 P2 P3 P4
0 2 4 6 8 1 1 1 1
0 2 4 6
P1’s & P2’s quanta are 0;
Time = 5
P4 is selected!
67
Round-robin
P1 P2 P3 P4 P1
0 2 4 6 8 1 1 1 1
0 2 4 6
Now, recharge is needed.
Time = 7
P1 is selected.
68
Round-robin
P1 P2 P3 P4 P1 P2
0 2 4 6 8 1 1 1 1
0 2 4 6
P1’s quantum is 0;
Time = 9
P2 is selected!
69
Round-robin
P1 P2 P3 P4 P1 P2 P4
0 2 4 6 8 1 1 1 1
0 2 4 6
P1’s quantum is 0;
Time = 11
P4 is selected!
70
Round-robin
P1 P2 P3 P4 P1 P2 P4 P1
0 2 4 6 8 1 1 1 1
0 2 4 6
Now, recharge is needed.
Time = 13
P1 is selected.
71
Round-robin
P1 P2 P3 P4 P1 P2 P4 P1 P1
0 2 4 6 8 1 1 1 1
0 2 4 6
Now, recharge is needed.
Time = 15
P1 is selected.
72
Round-robin
P1 P2 P3 P4 P1 P2 P4 P1 P1
0 2 4 6 8 1 1 1 1
0 2 4 6
Waiting time:
P1 = 9; P2 = 5; P3 = 0; P4 = 4;
Turnaround time: P2 2 4 0
P3 4 1 0
P1 = 16; P2 = 9; P3 = 1; P4 = 8; P4 5 4 0
73
RR VS SJF
Non-preemptive
Preemptive SJF RR
SJF
Average turnaround
8 7 8.5 (largest)
time
So, the RR algorithm gets all the bad! Why do we still need it?
The responsiveness of the processes is great under the RR algorithm. E.g., you
won’t feel a job is “frozen” because every job is on the CPU from time to time!
74
Round-robin
75
Observations on RR
• Modified versions of round-robin are implemented
in (nearly) every modern OS.
– Users run a lot of interactive jobs on modern OS-es.
– Users’ priority list:
• Number one - Responsiveness;
• Number two - Efficiency;
• In other words, “ordinary users” expect a fast GUI response
than an efficient scheduler running behind.
First-come, first-served
or First-in, First-out No. Out-of-date
(FIFO)
Priority scheduling
The real implementation!
with multiple queues.
77
Priority Scheduling
• Some basics:
– A task is given a priority (and is usually an integer).
– A scheduler selects the next process based on the
priority.
• A typical practice: the highest priority is always chosen.
– Special case: SJF, FCFS (equal priority)
78
Priority Scheduling
P2 P5 P1 P3 P4
0 2 4 6 8 1 1 1 1
0 2 4 6
Assumption:
79
Different algorithms
First-come, first-served
or First-in, First-out No. Out-of-date
(FIFO)
Priority scheduling
The real implementation!
with multiple queues.
80
Multilevel queue scheduling
• Definitions.
– It is still a priority scheduler.
– But, at each priority class, different schedulers may be
deployed.
– Eg: Foreground processes and background processes
Just an example.
Priority class 5 Non-preemptive, FIFO
81
Multilevel queue scheduling– an example
• Properties: process is assigned a fix priority when
they are submitted to the system.
Priority 4
Priority 3
Priority 2
Priority 1
82
Multilevel queue scheduling– an example
• The highest priority class will be selected.
– To prevent high-priority tasks from running indefinitely.
– The tasks with a higher priority should be short-lived, but
important;
Priority class 4
Priority class 3
Priority class 2
Priority class 1
83
Multilevel queue scheduling– an example
• Lower priority classes will be scheduled only when
the upper priority classes has no tasks.
Priority class 4
Priority class 3
Priority class 2
Priority class 1
84
Multilevel queue scheduling– an example
• Of course, it is a good design to have a high-priority
task preempting a low-priority task.
(conditioned that the high-priority task is short-lived.)
Priority class 4
Priority class 3
Priority class 2
Priority class 1
85
Multilevel queue scheduling– an example
• Any problem?
– Fixed priority
– Indefinite blocking or starvation
Priority class 4
Priority class 3
Priority class 2
Priority class 1
86
Multilevel feedback queue scheduling
• How to improve the previous scheme?
– Allows a process to move between queues (dynamic
priority).
– Why needed?
• Eg.: Separate processes according to their CPU bursts.
Just an example.
Priority class 5 Non-preemptive, FIFO
87
Multilevel feedback queue scheduling
• How to design (factors)?
– Number of queues
– Scheduling algorithm for each queue
– Method for determining when to upgrade/downgrade a
process
– Method for determining which queue a process will
enter
88
Summary
• Did we solve the conflict? Priority scheduler
guarantees this.
Policy
Big enforcement Big
conflict conflict
CPU-I/O
Fairness Balance
Little conflict
90
- Applications/Scenarios
- Real-time systems
- Multiple processors
- Example: Linux scheduler
- Algorithm evaluation
91
Real-time CPU Scheduling
Antilock brake system: Latency requirement: 3-5 ms
92
Real-time CPU Scheduling Algorithms
Rate monotonic scheduling
Assumption: Processes require CPU at constant periods: processing time t and period
p (rate 1/p)
Each process is assigned a priority proportional to its rate, and schedule processes
with a static priority policy with preemption (fixed priority)
Example
93
Real-time CPU Scheduling Algorithms
Rate monotonic scheduling Any problem?
Processes require CPU at constant periods: processing time t and period p (rate 1/p)
Each process is assigned a priority proportional to its rate, and schedule processes
with a static priority policy with preemption (fixed priority)
Example
94
Real-time CPU Scheduling Algorithms
Earliest-deadline-first scheduling (EDF)
Dynamically assigns priorities according to deadline (the earlier the deadline, the
higher the priority)
Example
EDF does not require the processes to be periodic, nor require a constant
CPU time per burst
95
- Applications/Scenarios
- Real-time systems
- Multiple processors
- Example: Linux scheduler
- Algorithm evaluation
96
Scheduling Issues with SMP
SMP: Each processor may have its private
queue of ready processes
Scheduling between processors
98
Linux Scheduler
• A multiple queue, (kind of) static priority scheduler.
Priorities 0 to 99 are
0 RR FIFO privileged classes.
99
Linux Scheduler
• A multiple queue, (kind of) static priority scheduler.
CFS
0 RR FIFO
-Each process maintains virtual run
... time (vruntime), recording how
long each has run
99 RR FIFO
-CFS selects the process that has
Norm the smallest vruntime value
CFS
al
-Decay factor: nice value (-20 to
+19): the smaller the value is, the
Completely Fair Scheduler
“higher priority” the process get
100
Linux Scheduler
• A multiple queue, (kind of) static priority scheduler.
CFS
0 RR FIFO
-Use a red-black tree to maintain
... runnable tasks
-The leftmost value is cached
99 RR FIFO
Norm CFS
al
101
- Applications/Scenarios
- Multiple processors
- Real-time systems
- Example: Linux scheduler
- Algorithm evaluation
102
How to select/evaluate a scheduling algorithm?
How to select a scheduling alg? (many algorithms with different parameters and properties)
Evaluate Algorithms
103
Summary on scheduling
• So, you may ask:
– “What is the best scheduling algorithm?”
– “What is the standard scheduling algorithm?”
104
Summary on part 2
User Space
Process Process Process
Kernel Space
Process Operations
(fork(),exec*(),wait()) Thread 1 Thread 2
Process
P Scheduler
105