Operating System - Process Scheduling
Operating System - Process Scheduling
Scheduling
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 6: CPU Scheduling
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Thread Scheduling
Multiple-Processor Scheduling
Real-Time CPU Scheduling
Operating Systems Examples
Algorithm Evaluation
Operating System Concepts – 9th Edition 6.2 Silberschatz, Galvin and Gagne ©2013
Objectives
Operating System Concepts – 9th Edition 6.3 Silberschatz, Galvin and Gagne ©2013
Basic Concepts
Operating System Concepts – 9th Edition 6.4 Silberschatz, Galvin and Gagne ©2013
Histogram of CPU-burst Times
Operating System Concepts – 9th Edition 6.5 Silberschatz, Galvin and Gagne ©2013
CPU Scheduler
Short-term scheduler selects from among the processes in
ready queue, and allocates the CPU to one of them
Queue may be ordered in various ways
Operating System Concepts – 9th Edition 6.6 Silberschatz, Galvin and Gagne ©2013
CPU Scheduler
scheduling in scenarios 2, 3 is preemptive
Operating System Concepts – 9th Edition 6.7 Silberschatz, Galvin and Gagne ©2013
CPU Scheduler
Points to consider/worry
Consider access to shared data
Operating System Concepts – 9th Edition 6.8 Silberschatz, Galvin and Gagne ©2013
Dispatcher
Operating System Concepts – 9th Edition 6.9 Silberschatz, Galvin and Gagne ©2013
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible
Throughput – # of processes that complete their execution per
time unit
(#Processes/hr for long processes and #Processes/sec for short
processes )
Turnaround time – amount of time to execute a particular
process
Waiting time – amount of time a process has been waiting in the
ready queue
Response time – amount of time it takes from when a request
was submitted until the first response is produced, not output (for
time-sharing environment)
Operating System Concepts – 9th Edition 6.10 Silberschatz, Galvin and Gagne ©2013
Scheduling Algorithm Optimization Criteria
Operating System Concepts – 9th Edition 6.11 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling
P1 P2 P3
0 24 27 30
Operating System Concepts – 9th Edition 6.12 Silberschatz, Galvin and Gagne ©2013
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P 2 , P3 , P 1
The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
Operating System Concepts – 9th Edition 6.13 Silberschatz, Galvin and Gagne ©2013
FCFS Scheduling (Cont.)
Given the burst times (BT) of the processes, for FCFS:
2 P3 3 23+3- (2-1)
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:
P1 P2 P3
0 24 27 30
Operating System Concepts – 9th Edition 6.15 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling
Process GT WT
P1 0 0 (0)
P2 3 0 (0+2- (3-0))
P3 27 23 (0+24 – (4-3))
P1 P2 P3
0 23
Operating System Concepts – 9th Edition
27 6.16
30
Silberschatz, Galvin and Gagne ©2013
FCFS Scheduling (Cont.)
Wait time using a formula for FCFS
WT(1) =0 (wait time (WT) for first process is 0)
Operating System Concepts – 9th Edition 6.17 Silberschatz, Galvin and Gagne ©2013
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
SJF is optimal – gives minimum average waiting time for a given
set of processes
The difficulty is knowing the length of the next CPU request
Could ask the user
Operating System Concepts – 9th Edition 6.18 Silberschatz, Galvin and Gagne ©2013
Example of SJF
P4 P1 P3 P2
0 3 9 16 24
Operating System Concepts – 9th Edition 6.19 Silberschatz, Galvin and Gagne ©2013
Determining Length of Next CPU Burst
Operating System Concepts – 9th Edition 6.20 Silberschatz, Galvin and Gagne ©2013
Examples of Exponential Averaging
=0
n+1 = n
Recent history does not count
=1
n+1 = tn
Only the actual last CPU burst counts
If we expand the formula, we get:
n+1 = tn+(1 - ) tn -1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
Operating System Concepts – 9th Edition 6.21 Silberschatz, Galvin and Gagne ©2013
Prediction of the Length of the Next CPU Burst
Operating System Concepts – 9th Edition 6.22 Silberschatz, Galvin and Gagne ©2013
Example of Shortest-remaining-time-first
P1 P2 P4 P1 P3
0 1 5 10 17 26
Operating System Concepts – 9th Edition 6.23 Silberschatz, Galvin and Gagne ©2013
Priority Scheduling
A priority number (integer) is associated with each process
Operating System Concepts – 9th Edition 6.24 Silberschatz, Galvin and Gagne ©2013
Example of Priority Scheduling
Operating System Concepts – 9th Edition 6.25 Silberschatz, Galvin and Gagne ©2013
Round Robin (RR)
Each process gets a small unit of CPU time (time quantum q),
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.
Timer interrupts every quantum to schedule next process
Performance
q large FIFO
q small q must be large with respect to context switch,
otherwise overhead is too high
Operating System Concepts – 9th Edition 6.26 Silberschatz, Galvin and Gagne ©2013
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Operating System Concepts – 9th Edition 6.27 Silberschatz, Galvin and Gagne ©2013
Time Quantum and Context Switch Time
Operating System Concepts – 9th Edition 6.28 Silberschatz, Galvin and Gagne ©2013
Turnaround Time Varies With The Time Quantum
Operating System Concepts – 9th Edition 6.29 Silberschatz, Galvin and Gagne ©2013
Multiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are
available
We concentrate on Homogeneous processors within a
multiprocessor- systems in which the processors are identical.
Operating System Concepts – 9th Edition 6.38 Silberschatz, Galvin and Gagne ©2013
Multiple-Processor Scheduling
Symmetric multiprocessing (SMP) – each processor is self-
scheduling, all processes in common ready queue, or each has
its own private queue of ready processes
Currently, most common: Windows, Linux, and Mac OS X
An architecture, CPU has faster access to some parts of
main memory than to other parts.
Typically, in systems containing combined CPU
and memory boards.
The CPUs on a board can access the memory on that
board faster than they can access memory on other boards in
the system.
If the CPU scheduler and memory-placement algorithms
work together---> a process that is assigned affinity to a
particular CPU can be allocated memory on the same board.
Operating System Concepts – 9th Edition 6.40 Silberschatz, Galvin and Gagne ©2013
NUMA and CPU Scheduling
Operating System Concepts – 9th Edition 6.41 Silberschatz, Galvin and Gagne ©2013
Multiple-Processor Scheduling – Load Balancing
Operating System Concepts – 9th Edition 6.42 Silberschatz, Galvin and Gagne ©2013
Multiple-Processor Scheduling – Load Balancing
Push and pull migration need not be mutually exclusive and are
in fact often implemented in parallel on load-balancing systems.
For example, the Linux scheduler and the ULE scheduler
available for Free BSD systems implement both techniques.
Operating System Concepts – 9th Edition 6.43 Silberschatz, Galvin and Gagne ©2013
Multicore Processors
Operating System Concepts – 9th Edition 6.44 Silberschatz, Galvin and Gagne ©2013
Multicore Processors
Operating System Concepts – 9th Edition 6.46 Silberschatz, Galvin and Gagne ©2013
Multithreaded Multicore System
2 ways to multithread a processing core: coarse-grained and fine-
grained multithreading.
Coarse-grained multithreading:
A thread executes on a processor until a long-latency event such as
a memory stall occurs.
The cost of switching between threads is high, since the
instruction pipeline must be flushed before the other thread can
begin execution on the processor core.
Once this new thread begins execution, it begins filling the pipeline
with its instructions.
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013