Principles of Operating Systems CS 446/646
4. CPU Scheduling
René Doursat Department of Computer Science & Engineering University of Nevada, Reno Spring 2006
Principles of Operating Systems CS 446/646 0. Course Presentation 1. Introduction to Operating Systems 2. Processes 3. Memory Management 4. CPU Scheduling 5. Input/Output 6. File System 7. Case Studies
3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
2
Principles of Operating Systems CS 446/646 4. CPU Scheduling a. Concepts of Scheduling b. Scheduling Algorithms c. Queuing Analysis d. Thread Scheduling
3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
3
Principles of Operating Systems CS 446/646 4. CPU Scheduling a. Concepts of Scheduling 9 Three-level scheduling 9 Purpose of CPU scheduling
b. Scheduling Algorithms c. Queuing Analysis d. Thread Scheduling
3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
4
4.a Concepts of Scheduling Three-level scheduling
¾ Medium-term scheduling 9 the decision to add to the number of processes that are partially or fully in main memory (“swapping”) 9 not the same as paging: swapping out means removing all the pages of a process
¾ Short-term scheduling = CPU scheduling 9 the decision as to which available processes in memory are to be executed by the processor (“dispatching”) 3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
5
frequency of intervention
9 the decision to add a program to the pool of processes to be executed: controls the degree of multiprogramming
fine- to coarse-grain time scale
¾ Long-term scheduling (mostly in batch)
4.a Concepts of Scheduling Three-level scheduling
¾ Three-level scheduling
SHORT-TERM
Tanenbaum, A. S. (2001) Modern Operating Systems (2nd Edition).
LONG-TERM
MID-TERM
Three-level scheduling 3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
6
4.a Concepts of Scheduling Three-level scheduling
¾ Reminder: process states
dispatch
admit
Ready
ti v at ac
admit
s
Suspended Ready
nd e p us
e ti v at ac
program is on disk 3/28/2006
event occurs
Blocked
event occurs
Suspended Blocked
Running
release
Exit
timeout
e
New
s
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
event wait program is in memory
nd e p us
Transition diagram of a seven-state model CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
7
4.a Concepts of Scheduling Three-level scheduling
¾ In the O/S, the CPU scheduler decides which “Ready” process to run next (and which “Running” to time out) 9 the discipline it follows is the scheduling algorithm New
Ready
Running
Exit
LONG-TERM Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
SHORT-TERM Suspended Ready MID-TERM
program is on disk 3/28/2006
Suspended Blocked
Blocked
program is in memory
Transition diagram of a seven-state model CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
8
4.a Concepts of Scheduling Three-level scheduling
¾ General queuing system for scheduling 9 in most algorithms, queues are not strictly FIFO: rather “pools”
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
Queuing diagram for scheduling 3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
9
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ Why scheduling matters: user service response 9 example: choosing between a process that updates the screen after the user has closed a window a process that sends out queued email 9 taking 2 seconds to close the window while sending the email would be unacceptable 9 on the other hand, delaying the email while closing the window would hardly be noticed → schedule wisely to match user’s expectations
3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
10
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ Why scheduling matters: CPU usage 9 switching processes (contexts) is heavy switch from user mode to kernel mode CPU state must be saved process state must be saved pages and page bits must be saved MMU must be reloaded with new page table etc. → to maximize CPU utilization, interleave but at the same time minimize process switches
3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
11
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ Types of process behavior: CPU-I/O burst cycle 9 processes alternate CPU usage with I/O wait compute-bound processes have long CPU bursts and infrequent I/O I/O-bound processes have short CPU bursts and frequent I/O
Tanenbaum, A. S. (2001) Modern Operating Systems (2nd Edition).
(a) Compute-bound process vs. (b) I/O-bound process 3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
12
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ Types of process behavior: CPU-I/O burst cycle 9 power-law: large # of short CPU bursts, small # of large bursts
Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).
3/28/2006
Typical histogram of CPU-burst times
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
13
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ I/O-bound processes 9 as CPU speeds increase, processes generally tend to become more and more I/O-bound 9 the scheduling of I/O-bound processes will likely become an important subject in the future → basic idea: an I/O-bound process that is “Ready” to run should get the CPU quickly so it can keep the disk busy
3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
14
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ When scheduling decisions must be made 9 when a new process is created — run the child or the parent? 9 when a process exits — who’s next? 9 when an I/O interrupt occurs upon finishing an I/O task — should the waiting process be rescheduled? or let the currently running process continue? or pick another process? etc. 9 when a timeout (clock interrupt) occurs — who’s next?
3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
15
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ Two kinds of CPU-scheduling algorithms 9 cooperative scheduling — let a process run until it blocks on I/O, terminates or voluntarily releases the CPU (system call) Ready
Running
Blocked
9 preemptive scheduling — follow clock interrupts (ex: 50Hz) to forcibly switch processes (demote the “Running” to “Ready”) Ready
Running
Blocked 3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
16
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ Scheduling algorithm goals — all systems (batch & int.) 9 fairness — comparable processes get comparable service 9 compliance to system’s policy — ex: high-priority override low-priority processes (ex: safety control vs. payroll in a nuclear plant) 9 keep system busy — CPU and I/O devices should be utilized fully if all CPU-bound were run first: fight for CPU, I/O idle then all I/O-bound were run: fight for I/O, CPU idle → keep a well-balanced mix of CPU-bound and I/O-bound processes, so they can fill in for each other 3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
17
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ Scheduling algorithm goals — batch systems 9 throughput — maximize # of completed jobs per time unit 9 turnaround time (latency) — minimize time between submission and termination of job high throughput and low turnaround are rarely compatible for ex: supply of short jobs scheduled in front of long jobs: good throughput, bad turnaround time for long jobs Arrival times
Execution times
#5 arrived
thr
ou gh pu t
#5 executed turnaround time for #5
3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
18
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ Scheduling algorithm goals — interactive systems 9 response time — respond to requests quickly: minimize time between issuing command and getting result ex: a user request to start a new program should take precedence over background work having interactive requests go first will be perceived as good service 9 proportionality time — meet users’ expectation, even if irrational ex: 45 seconds to establish a modem connection is perceived as acceptable, yet 45 seconds to hang up is not whenever possible, take this into account when scheduling
3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
19
4.a Concepts of Scheduling Purpose of CPU scheduling
¾ Scheduling algorithm goals — summary
Tanenbaum, A. S. (2001) Modern Operating Systems (2nd Edition).
Some goals of CPU scheduling under different circumstances 3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
20
Principles of Operating Systems CS 446/646 4. CPU Scheduling a. Concepts of Scheduling 9 Three-level scheduling 9 Purpose of CPU scheduling
b. Scheduling Algorithms c. Queuing Analysis d. Thread Scheduling
3/28/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
21
Principles of Operating Systems CS 446/646 4. CPU Scheduling a. Concepts of Scheduling b. Scheduling Algorithms 9 Scheduling in batch systems 9 Scheduling in interactive systems
c. Queuing Analysis d. Thread Scheduling
3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
22
4.b Scheduling Algorithms Scheduling in batch systems
¾ Scheduling metrics 9 9 9 9
arrival time ta = time the process became “Ready” (again) wait time Tw = time spent waiting for the CPU service time Ts = time spent executing in the CPU turnaround time Tr = total time spent waiting and executing = Tw + Ts ta
Arrival times
#5 arrived
Tw
Ts
Execution times
Tr 3/30/2006
#5 executed
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
Tr / Ts = 2.5 23
4.b Scheduling Algorithms Scheduling in batch systems
¾ First-Come-First-Served (FCFS) 9 processes are assigned the CPU in the order they request it 9 when the running process blocks, the first “Ready” is run next 9 when a process gets “Ready”, it is put at the end of the queue A B
Arrival times
C D E
A
B
C
D
FCFS scheduling policy 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
E
Mean
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
24
4.b Scheduling Algorithms Scheduling in batch systems
¾ First-Come-First-Served (FCFS) 9 nonpreemptive, oldest and simplest to program 9 apparently “fair” but very inefficient; example: a CPU-bound process runs 1 sec, then reads 1 disk block several I/O-bound processes run little CPU, but must read 1000 disk blocks CPU-bound
1 sec
I/O-bound processes
. . . because of the CPU-bound, one I/O-bound will take 1000 seconds
→ preempt the CPU-bound more often to let the I/O-bound progress 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
25
4.b Scheduling Algorithms Scheduling in batch systems
CPU-bound I/O-bound processes
. . .
interleaving’s squeeze
by preempting the CPU-bound every 10ms (100 Hz), each I/O-bound now takes only 10 seconds (without bothering the CPU-bound too much ~10s)
→ see preemptive algorithms (Round-Robin, etc.) in later sections 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
26
4.b Scheduling Algorithms Scheduling in batch systems
¾ Shortest Job First (SJF) 9 nonpreemptive, assumes the run times are known in advance 9 among several equally important “Ready” jobs (or CPU bursts), the scheduler picks the one that will finish the earliest A B
Arrival times
C D E
Shortest Job First (SJF)
SJF
A
B
C
D
SJF scheduling policy 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
E
Mean
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
27
4.b Scheduling Algorithms Scheduling in batch systems
¾ Shortest Job First (SJF) 9 example:
no SJF
SJF
a) turnaround times Tr = 8, 12, 16, 20 → mean Tr = 14 b) turnaround times Tr = 4, 8, 12, 20 → mean Tr = 11 9 SJF is optimal among jobs available immediately; proof: generally, with service times Ts = a, b, c, d the mean turnaround time is: Tr = (4a + 3b + 2c + d) / 4, therefore it is always better to schedule the longest process (d) last 9 however, being non-preemptive, SJF does not deal well with jobs arriving subsequently (ex: 2,4,1,1,1 arriving at 0,0,3,3,3) 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
28
4.b Scheduling Algorithms Scheduling in batch systems
¾ Shortest Remaining Time (SRT) 9 preemptive version of SJF, also assumes known run time 9 choose the process whose remaining run time is shortest 9 allows new short jobs to get good service A B
Arrival times
C D E
A
B
C
D
SRT scheduling policy 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
E
Mean
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
29
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Round-Robin (RR) 9 preemptive FCFS, based on a timeout interval, the quantum q 9 the running process is interrupted by the clock and put last in a FIFO “Ready” queue; then, the first “Ready” process is run instead A B
Arrival times
C D E
A
B
C
D
RR (q = 1) scheduling policy 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
E
Mean
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
30
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Round-Robin (RR) 9 a crucial parameter is the quantum q (generally ~10–100ms) q should be big compared to context switch latency (~10µs) q should be less than the longest CPU bursts, otherwise RR degenerates to FCFS → typically at 80% of the distrib. tail A B
Arrival times
C D E
A
B
C
D
RR (q = 4) scheduling policy 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
E
Mean
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
31
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Shortest Process Next (SPN) 9 same as SJF: pick the one that should finish the earliest → difference in the interactive system: the prediction about future duration is not known but estimated from past durations A B
Arrival times
C D E
Shortest Process Next (SPN)
A
B
C
D
SPN scheduling policy 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
E
Mean
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
32
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Estimation of processing time from past 9 predicted service time = simple averaging of past run times
S(n + 1) = (1 / n) ∑ T(i)
⇔ S(n + 1) = T(n) / n + (1 – 1/ n) S(n) 9 exponential averaging, also called “aging”
3/30/2006
S(n + 1) = α T(n) + (1 – α) S(n), 0 < α ≤ 1 high α forgets past runs quickly low α remembers past runs for a long time
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
33
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Estimation of processing time from past 9 “aging” tracks changes in process behavior faster than the mean
Example of exponential averaging in duration estimation 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
34
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Highest Response Ratio Next (HRRN) 9 minimize the normalized turnaround time Tr / Ts → compromise between FCFS, which favors long processes, and SPN, which favors short processes A B
Arrival times
C D E
A
B
C
D
HRRN scheduling policy 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
E
Mean
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
35
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Priority Scheduling 9 several “Ready” process queues, with different priorities
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
Priority queuing 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
36
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Priority Scheduling 9 processes are assigned to queues based on their properties (memory size, priority, bound type, etc.)
Multilevel queue scheduling 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
Silberschatz, A., Galvin, P. B. and Gagne. G. (2003) Operating Systems Concepts with Java (6th Edition).
37
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Priority Scheduling with Feedback (FB) 9 processes can be moved among queues 9 each queue has its own policy, generally RR with variable q(Qi)
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
Priority queuing 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
38
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Priority Scheduling with Feedback (FB) 9 each time a process is preempted, it is demoted to a lowerlevel queue 9 tends to leave I/O-bound in higher priority queues, as desired A B
Arrival times
C D E
A
B
C
D
FB (q = 1) scheduling policy 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
E
Mean
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
39
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Priority Scheduling with Feedback (FB) 9 a uniform RR quantum for all queues might create starvation 9 to compensate for increasing wait times in lower queue, increase q, too; for example q = 2i A B
Arrival times
C D E
A
B
C
D
FB (q = 2i) scheduling policy 3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
E
Mean
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
40
4.b Scheduling Algorithms
Stallings, W. (2004) Operating Systems: Internals and Design Principles (5th Edition).
Scheduling in interactive systems
3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
41
4.b Scheduling Algorithms Scheduling in interactive systems
¾ Traditional UNIX scheduling 9 9 9 9
3/30/2006
multilevel feedback using RR within each of the priority queues typically 1-second preemption timeout system of integer priorities recomputed once per second a base priority divides processes into fixed bands of priority levels; in decreasing order: swapper block I/O device control file manipulation character I/O device control user processes CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
42
Principles of Operating Systems CS 446/646 4. CPU Scheduling a. Concepts of Scheduling b. Scheduling Algorithms 9 Scheduling in batch systems 9 Scheduling in interactive systems
c. Queuing Analysis d. Thread Scheduling
3/30/2006
CS 446/646 - Principles of Operating Systems - 4. CPU Scheduling
43