Uos2005 Chp5
Uos2005 Chp5
Uos2005 Chp5
CPU Scheduling
Objective:
Basic Scheduling Concepts CPU Scheduling Algorithms
Why Multiprogramming?
Maximize CPU/Resources Utilization (Based on Some Criteria)
CPU Scheduling
Process Execution
CPU-bound programs tend to have a few very long CPU bursts. IO-bound programs tend to have many very short CPU bursts.
CPU-Burst
New Terminate
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
I/O-Burst
CPU Scheduling
The distribution can help in selecting an appropriate CPU-scheduling algorithms
120 100 60 20 8
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
frequency
CPU Scheduling
CPU Scheduler The Selection of Process for Execution
A short-term scheduler
New dispatched Ready Waiting
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Terminated
Running
CPU Scheduling
Nonpreemptive Scheduling
A running process keeps CPU until it volunteers to release CPU
E.g., I/O or termination
Advantage
Easy to implement (at the cost of service response to other processes)
CPU Scheduling
Preemptive Scheduling
Beside the instances for non-preemptive scheduling, CPU scheduling occurs whenever some process becomes ready or the running process leaves the running state!
Issues involved:
Protection of Resources, such as I/O queues or shared data, especially for multiprocessor or real-time systems. Synchronization
E.g., Interrupts and System calls
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
CPU Scheduling
Dispatcher
Functionality:
Switching context Switching to user mode Restarting a user program
Dispatch Latency:
Must be fast Stop a process
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Start a process
Scheduling Criteria
Why?
Different scheduling algorithms may favor one class of processes over another!
Criteria
CPU Utilization Throughput Turnaround Time: CompletionT-StartT Waiting Time: Waiting in the ReadyQ Response Time: FirstResponseTime
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Scheduling Criteria
How to Measure the Performance of CPU Scheduling Algorithms? Optimization of what? General Consideration
Average Measure Minimum or Maximum Values
Variance
Predictable Behavior
Scheduling Algorithms
First-Come, First-Served Scheduling (FIFO) Shortest-Job-First Scheduling (SJF) Priority Scheduling Round-Robin Scheduling (RR) Multilevel Queue Scheduling Multilevel Feedback Queue Scheduling Multiple-Processor Scheduling
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
dispatched
*The average waiting time is highly affected by process CPU burst times !
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
I/O device
idle
ready queue
P4 P1 0 3
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
n+1 = tn + (1-) n
P1 P2 P4 P1 P3 10 17 26 0 1 5
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Priority Scheduling
CPU is assigned to the process with the highest priority A framework for various scheduling algorithms:
FCFS: Equal-Priority with TieBreaking by FCFS SFJ: Priority = 1 / next CPU burst length
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Priority Scheduling
Process P1 P2 P3 P4 P5 CPU Burst Time 10 1 2 1 5 Priority 3 1 3 4 2
Gantt Graph P2 P5 0 1
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Priority Scheduling
Priority Assignment
Internally defined use some measurable quantity, such as the # of open files, Average CPU Burst
Average I/O Burst
Externally defined set by criteria external to the OS, such as the criticality levels of jobs.
Priority Scheduling
Preemptive or Non-Preemptive?
Preemptive scheduling CPU scheduling is invoked whenever a process arrives at the ready queue, or the running process relinquishes the CPU. Non-preemptive scheduling CPU scheduling is invoked only when the running process relinquishes the CPU.
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Priority Scheduling
Major Problem
Indefinite Blocking (/Starvation)
Low-priority processes could starve to death!
A Solution: Aging
A technique that increases the priority of processes waiting in the system for a long time.
FIFO
The quantum is used up!
New process
quantum = 1
10 10 10 20 20 20 30 30 30
Average Turnaround Time = (10+20+30)/3 = 20 => 80% CPU Burst < time slice
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
ATT = (28+29+30)/3 = 29
Batch Processes
Inter-queue scheduling
a. Fixed-priority preemptive scheduling
a. e.g., foreground queues always have absolute priority over the background queues.
c. More??
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
FCFS
*Idea: Separate processes with different CPU-burst characteristics!
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Multiple-Processor Scheduling
CPU scheduling in a system with multiple CPUs A Homogeneous System
Processes are identical in terms of their functionality.
Can processes run on any processor?
A Heterogeneous System
Programs must be compiled for instructions on proper processors.
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Multiple-Processor Scheduling
Load Sharing Load Balancing!!
A queue for each processor
Self-Scheduling Symmetric Multiprocessing
Multiple-Processor Scheduling
Load Balancing
Push migration: A specific task periodically checks for imbalance and migrate tasks Pull migration: An idle processor pulls a waiting task from a busy processor Linux and FreeBSD do both!
Processor Affinity
The system might avoid process migration because of the cost in invalidating or repopulating caches Soft or hard affinity
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Multiple-Processor Scheduling
Symmetric Multithreading (SMT), i.e., Hyperthreading
A feature provided by the hardware Several logical processors per physical processor
Each has its own architecture state, including registers.
: 13
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
: 10
: 11
Thread Scheduling
Two Scopes:
Process Contention Scope (PCS): m:1 or m:m
Priority-Driven
Pthread Scheduling
PCS and SCS
Pthread_attr_setscope(pthread_attr_t *attr, int scope) Pthread_attr_getscope(pthread_attr_t *attr, int *scope)
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Time-Sharing
low A default class
Interactive
System
The priorities of system processes are fixed.
Time-Sharing
Multilevel feedback queue scheduling priorities inversely proportional to time slices
Interactive
Prefer windowing process
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
low
Fair Sharing
CPU shares, instead of priorities
Idle thread
High
31 26 25 24 23 22 16
15 15 14 13 12 11 1
15 12 11 10 9 8 1
15 10 9 8 7 6 1
15 8 7 6 5 4 1
15 6 5 4 3 2 1
Base Priority
Real-Time Class
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Scheduling Algorithm
O(1) SMP, load balancing, and processor affinity Fairness and support for interactive tasks Priorities
Real-time: 0..99 Nice: 100..140
200ms . . . . . . . . 10ms
Algorithm Evaluation
A General Procedure
Select criteria that may include several measures, e.g., maximize CPU utilization while confining the maximum response time to 1 second Evaluate various algorithms
Evaluation Methods:
Deterministic modeling Queuing models Simulation Implementation
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Deterministic Modeling
A Typical Type of Analytic Evaluation
Take a particular predetermined workload and defines the performance of each algorithm for that workload
Properties
Simple and fast Through excessive executions of a number of examples, trends might be identified But it needs exact numbers for inputs, and its answers only apply to those cases
Being too specific and requires too exact knowledge to be useful!
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Deterministic Modeling
FCFC
P1 0
process CPU Burst time P1 10 P2 29 P3 3 P4 7 P5 12
P2 10
P3 P4 39 42 49
P5 61
P3 P4 P1 P5 0 3 10 20 32
AWT=(10+32+0+3+20)/5=13 Round Robin (quantum =10)
P2 61
P1 0
P2 P3 P4 P5 P2 P5 P2 10 2023 30 40 50 52 61
AWT=(0+(10+20+2)+20+23+(30+10))/5=23
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Queueing Models
Motivation:
Workloads vary, and there is no static set of processes
Service rate
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Queueing Models
Model a computer system as a network of servers. Each server has a queue of waiting processes
Compute average queue length, waiting time, and so on.
Properties:
Generally useful but with limited application to the classes of algorithms & distributions Assumptions are made to make problems solvable => inaccurate results
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Queueing Models
Example: Littles formula
n = w
w
steady state!
n = # of processes in the queue = arrival rate = average waiting time in the queue If n =14 & =7 processes/sec, then w = 2 seconds.
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Simulation
Motivation:
Get a more accurate evaluation.
Procedures:
Program a model of the computer system Drive the simulation with various data sets
Randomly generated according to some probability distributions
=> inaccuracy occurs because of only the occurrence frequency of events. Miss the order & the relationships of events.
Trace tapes: monitor the real system & record the sequence of actual events.
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.
Simulation
Properties:
Accurate results can be gotten, but it could be expensive in terms of computation time and storage space. The coding, design, and debugging of a simulator can be a big job.
Implementation
Motivation:
Get more accurate results than a simulation!
Procedure:
Code scheduling algorithms Put them in the OS Evaluate the real behaviors
Implementation
Difficulties:
Cost in coding algorithms and modifying the OS Reaction of users to a constantly changing the OS The environment in which algorithms are used will change
For example, users may adjust their behaviors according to the selected algorithms => Separation of the policy and mechanism!
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2005.