CPU Scheduling
CPU Scheduling
CPU Scheduling
1
Objectives and Outline
Objective
Outline
• ToBasic
introduce
ConceptsCPU scheduling, which is the basis for multi-programmed (multi-
• tasking) operating
Scheduling Criteriasystems
•• To describe Algorithms
Scheduling various CPU-scheduling algorithms
•• To discuss
Thread evaluation criteria for selecting a CPU-scheduling algorithm for a
Scheduling
• particular system Scheduling
Multiple-Processor
• Operating Systems Examples
• Algorithm Evaluation
2
Basic Concepts
3
Histogram of CPU-burst Times
4
CPU Scheduler
Dispatch latency
– switching context save state
into PCB1
– switching to user mode
– jumping to the proper location in the user reload state
from PCB2
program to restart that program
P2
executing
• Dispatch latency – time it takes for the
dispatcher to stop one process and start
another running
6
Linux tools
• at the bottom of the output you will see the number of context
switches that a process experiences. Like the following:
voluntary_ctxt_switches: 9
nonvoluntary_ctxt_switches: 18
7
Scheduling Criteria
• CPU
Maximize
utilization
CPU utilization
– keep the CPU as busy as possible
• Maximize
Throughputthroughput
– # of processes that complete their execution per
time unit
• Turnaround time – amount
Minimize turnaround time of time to execute a particular
• process
Minimize waiting time
8
Scheduling Criteria
ready running
waiting
Request submitted
10
First-Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
11
FCFS Scheduling (Cont)
P2 P3 P1
0 3 6 30
• Waiting time for P1 = 6; P2 = 0; P3 = 3
• Average waiting time: (6 + 0 + 3)/3 = 3 ms
• Much better than previous case
• Convoy effect: short process behind long process
12
Shortest-Job-First (SJF) Scheduling
• Associate with each process the length of its next CPU burst. Use these
lengths to schedule the process with the shortest time
13
Example of SJF
P4 P1 P3 P2
0 3 9 16 24
• Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 ms
14
Determining Length of Next CPU Burst
15
Predicting the Length of next CPU Burst:
Exponential Averaging
• Let tn denotes the length of the nth CPU burst.
– This is the actual length (known after burst executes)
• Let n+1 denote the predicted value for the next CPU burst
• Assume the first CPU burst is Burst0 and its length is t0.
• n denotes the previous estimate (before tn )
• Define to be:
0 <= <= 1
n+1 = tn + (1 - ) n
16
Exponential Averaging
• If =0
– n+1 = n
– Recent history does not count
• If =1
– n+1 = tn
– Only the actual last CPU burst counts
18
Example
• 0 = 10 ms
• Measured CPU bursts: t0 = 8ms, t1=16ms, t2=20ms, t3=10ms
• Assume = ½
– 1 = ½ x 8 + ½ x 10 = 9
– 2 = ½ x 16 + ½ x 9 = 12.5
– 3 = ½ x 20 + ½ x 12.5 = 16.25
– 4 = ½ x 10 + ½ x 16.25 = 13.125
– The next CPU burst is estimated to be 13.125 ms. After burst is executed,
it is measured as t4.
19
Shortest Remaining Time First (SRTF)
20
Shortest Remaining Job First (SRTF)
P1 P2 P4 P1 P3
0 1 5 10 17 26
21
Example
• Assume we have the following processes. Find out the finish time, waiting time and
turnaround time of each process for the following scheduling algorithms: FCFS, SJF,
SRJF.
22
Example
FCFS: Processes will run in the order they arrive. The following is the finish,
turnaround, waiting time of each process.
B 5 20 50 45 25
C 10 12 62 52 40
D 15 10 72 57 47
23
Example
B 5 20 72 67 47
C 10 12 52 42 30
D 15 10 40 25 15
24
Example
SRTF: running order will be: A(5) B(5) C(12) D(10) B(15) A(25)
B 5 20 47 42 22
C 10 12 22 12 0
D 15 10 32 17 7
25
Round Robin (RR)
• Each process gets a small unit of CPU time (time quantum – time
slice), usually 10-100 milliseconds. After this time has elapsed,
the process is preempted and added to the end of the ready
queue.
• If there are n processes in the ready queue and the time quantum
is q, then each process gets 1/n of the CPU time in chunks of at
most q time units at once.
• No process waits more than (n-1)q time units.
26
Round Robin (RR)
• Performance
– q large FIFO
– q small good CPU sharing
q must be large with respect to context switch time, otherwise
overhead is too high
27
Example of RR with Time Quantum = 4
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
28
Example
29
Example
Solution
30
RR vs FCFS
• Round Robin is good for fast response, not for low turnaround
time.
Assume 3 jobs all arrived at time 0. Each has a CPU burst = 10
C C C
B B B
A A A
RR q=5 FCFS
A: 20 A: 10
B: 25 B: 20
C: 30 C: 30
Turnaround times Turnaround times
31
Time Quantum and Context Switch Time
32
Turnaround Time Varies
With The Time Quantum
33
Priority Scheduling
• SJF is a priority scheduling (non-preemptive) where priority is the predicted next CPU
burst time.
• SRTF is preemptive
34
Example
36
Priority Scheduling
P1 4 (3)
P2 5 (2)
P3 8 (2) P4 P2 P3 P2 P3 P 2 P 3 P 1 P 5 P 1 P5
P4 7 (1)
P5 3 (3) 0 7 9 11 13 15 16 20 22 24 26 27
(priority)
Time quantum=2
burst time
37
Multilevel Queue Scheduling
38
Multilevel Queue Scheduling
39
Multilevel Queue Scheduling
Priority = 1 T7 T8 T9
40
Multilevel Queue Scheduling
Highest priority
System processes
Interactive processes
Batch processes
Lowest priority
41
Multilevel Feedback Queue (MLFQ) scheduling
42
Example of Multilevel Feedback Queue
• Three queues:
– Q0 – RR with time quantum 8 milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS
• Scheduling
– A new job enters queue Q0 which is served RR (q=8). When
it gains CPU, job receives 8 milliseconds. If it does not
finish in 8 milliseconds, job is moved to queue Q1.
– At Q1 job is again served RR and receives 16 additional
milliseconds. If it still does not complete, it is preempted and
moved to queue Q2.
– At Q2, jobs are served with FCFS 43
Multilevel Feedback Queues
44
Thread Scheduling
45
Thread Scheduling
• Distinction between user-level and kernel-level threads
46
Pthread Scheduling
47
Pthread Scheduling API
#include <pthread.h>
#include <stdio.h>
#define NUM THREADS 5
int main(int argc, char *argv[])
{
int i;
pthread t tid[NUM THREADS];
pthread attr t attr;
/* get the default attributes */
pthread attr init(&attr);
/* set the scheduling algorithm to PROCESS or SYSTEM */
pthread attr setscope(&attr, PTHREAD_SCOPE_SYSTEM);
/* set the scheduling policy - FIFO, RT, or OTHER */
pthread attr setschedpolicy(&attr, SCHED_OTHER);
/* create the threads */
for (i = 0; i < NUM THREADS; i++)
pthread create(&tid[i],&attr,runner,NULL);
48
Pthread Scheduling API
49
Multiprocessor Scheduling
50
Multiple-Processor Scheduling
51
Multiprocessor scheduling
52
Multiple-Processor Scheduling
SMP
sch …
P2 P10
Kernel P1 P3
Main Memory
53
SMP
54
Multicore Processors
55
Multithreaded Multicore System
56
Multithreaded Multicore System
• Chip-multithreading (CMT)
assigns each core multiple hardware
threads. (Intel refers to this as
hyperthreading.)
57
NUMA and CPU Scheduling
58
Processor Affinity
59
Load Balancing
• If load is unbalanced:
– Load balancing among the cores in the same chip can be
performed.
– Load balancing between different NUMA processors is not
desirable.
• Cache needs to be populated
• Access to memory in other chip is slower.
60
Heterogenous Multiprocessing (HMP)
61
Real-Time CPU Scheduling
62
Real time systems
63
Latency
64
Scheduling
65
Hard guarantees
– Periodic tasks:
• period (p) and rate (1/p),
• cpu time (t),
• deadline (d);
We must have: 0 <= t <= d <= p
• Example scheduling: Rate monotonic scheduling
66
Rate monotonic scheduling
deadline missed
68
Earliest Deadline First (EDF)
69
Earliest Deadline First (EDF)
70
Proportional Share Scheduling
71
Examples from Operating Systems
72
Operating System Examples
• Solaris scheduling
• Windows scheduling
• Linux scheduling
73
Solaris Dispatch Table
74
Solaris Scheduling
75
Windows Priorities
76
Linux Scheduling
77
Linux Scheduling – CFS Algorithm
• In time sharing:
– user can specify a nice value (priority value)
• between -20 and +19. (-20 is highest priority)
– CFS assigns a proportion of CPU time based on nice value
indirectly.
• Lower nice value -> higher CPU time given at the end
– Targeted latency: a time interval each ready process must run
once
• Interval allocated proportionally among processes
• Example: if there are 5 processes with equal priority in
ready state, and targeted latency (i.e., sched_latency) is
100ms, then each process will get a time slice of 20 ms.
78
Linux Scheduling – CFS Algorithm
• For time sharing class, CFS does not assign priorities directly.
– There are no separate runqueues.
• Keep track how long each process has run as virtual runtime.
• Virtual runtime is advanced more for a lower priority (higher
nice value) process, less for a higher priority process.
– Hence decay for low priority is higher.
• Example: 200 ms actual run time will be more for low
priority process, less for high priority process, same for
nice value 0.
• Select the process that has the lowest virtual runtime.
79
Linux Scheduling – CFS Algorithm
80
Linux Scheduling – CFS Algorithm
81
Algorithm Evaluation
82
Algorithm Evaluation
83
Evaluation of CPU schedulers by Simulation
84
Queuing Models
• Average throughput, utilization, and waiting time can be
computing by using queuing models for a system
– In this case, one or more CPUs and one or more ready queues
make the system.
• Arrivals of tasks modeled by a distribution (inter-arrival times)
– Exponential distribution is a good approximation for
stochastic arrivals (as opposed to deterministic arrivals)
• Service times (cpu times) modeled by a distribution
– Exponential distribution is a good approximation for CPU
times required (i.e., services times).
• A branch of mathematics called queuing networks analysis uses
such modes to analyze systems for waiting time, throughput, etc.
85
Queuing Models
• Little’s formula relates the arrival rate, waiting time,
and queue length in a system that may have random l
arrivals and random service (CPU) times.
• Little’s formula: N = W * l
– N: number of tasks in the queue (excluding the
one in the cpu)
– W: the average waiting in the queue for a task
(excluding the time spent in CPU)
cpu
– l: arrival rate (number of tasks arriving per
second)
• Assumes a stable system (service rate > arrival rate)
• Valid for any arrival and service time distribution
86
Queuing Models
• Example: Assume arrival rate is 30 tasks/sec. What is the
average queue length (excluding the task executing), if on the
average a task waits 200 ms in the system (excluding the cpu
execution)?
• Answer:
– N=W*l
– Therefore, N = 200 ms * 30 tasks/sec = 6. That means there
are 6 tasks in the system on the average, that are waiting in
the queue.
• Note that: if arrivals and service times are deterministic, no queuing and
therefore no waiting in the queue will happen. Waiting in the queue happens
due to random (stochastic) arrivals, i.e., bursty arrivals.
87
References
• The slides here are adapted/modified from the textbook and its slides:
Operating System Concepts, Silberschatz et al., 9th edition, Wiley.
• Operating System Concepts, 9th edition, Silberschatz et al. Wiley.
• Modern Operating Systems, Andrew S. Tanenbaum, 3rd edition, 2009
88