Sei sulla pagina 1di 26

CPU Scheduling

Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Algorithm Evaluation
Multiple-Processor Scheduling
Real-Time Scheduling
Basic Concepts - Scheduling

What is scheduling ?
Assignment of various jobs to the CPU to get the optimal use by
reducing CPU idling time

How is it possible to achieve the optimal use by job scheduling?


CPU is not always doing any processing
Some time is spent on I/O operations like displaying data
During this time the CPU idles (if not assigned another processing
job)

Scheduling is a fundamental and essential OS function in


multiprogramming environment the core of any OS, and all
computer resources including the CPU usage are scheduled
before use.
Basic Concepts CPU & I/O Burst Cycles

Fundamental Observation:

Process execution consists


of alternating cycles of

CPU execution (Busy CPU)


I/O wait (Idling CPU)

This is the property that makes


the Scheduling useful
Basic Concepts - Typical Histogram of
CPU Burst Times
Depends on various factors (type of job, hardware etc.) but
follows a common pattern
I/O bound process many short CPU bursts; CPU bound
process few long CPU bursts
Useful in CPU scheduling
Basic Concepts - CPU Scheduler &
Dispatcher

Scheduler - schedules processes (or threads in a process) for


execution depending on queue organization ( FIFO, priority
etc.)

CPU scheduling decisions may happen when a process /


thread:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates

Dispatcher - part of the scheduler that gives control of the CPU


to the scheduled process

Dispatch latency time it takes for the dispatcher to stop one


process and start another process running.
Basic Concepts Nonpreemptive vs.
Preemptive Scheduling

In cases 1 & 4 OS must schedule In cases 2 & 3 scheduling is


a new process (nonpreemptive preemptive (a preference may be
no preference to others) made; could be suspended)
Under nonpreemptive scheduling Needs synchronization for
process uses CPU until process cooperation (Ex. In case
termination or I/O request (no of 2 processes sharing data)
preference to others) Requires timer interrupt h/w
Simple & does not require special Kernel might not be preemptive
h/w i.e. cannot preempt process in the
Suitathble for batch or single- midst of a system call
user systems Suitable for multi-user timesharing
Used in MS Windows 3.1 & Apple systems
Mac Used in MS Windows
95/98/NT/2000 & all types of Unix
Scheduling Criteria Parameters

CPU utilization efficiency


Throughput number of completed processes per time
unit
Turnaround time time to execute a particular process
(including outputting time)
Waiting time waiting time of a process in the ready
queue
Response time time it takes from request initiation until
the first response is produced (excluding outputting time)
Fairness guarantee that every process gets a fair
share of CPU time
Scheduling Criteria Parameter
Optimization

Maximize
CPU utilization
Throughput

Minimize
Turnaround time
Waiting time
Response time

Ensure fairness
Scheduling Algorithms - General

Various algorithms proposed for CPU scheduling

Some optimisation criteria are contradictory!


e.g. maximising efficiency reduces fairness

Most algorithms concentrate on a particular aspect


Scheduling Algorithms 1. First Come
First Served (FCFS)

Simplest algorithm Service jobs in order of arrival


Non-premptive
Can be implemented by maintaining ready queue as
FIFO
Disadvantages
Long average waiting times & large variance in waiting
times
Convoy effect: one CPU bound long process will slow down
all other processes
Scheduling Algorithms 1. First Come
First Served (FCFS) - Example
Process Burst Time
P1 24
P2 3
P3 3

Schedule Gantt Chart Avg. wait time

(0 + 24 + 27)/3 = 17
P1 P2 P3
P1 , P2 , P3
0 24 27 30
P2 , P3 , P1 P2 P3 P1 (6 + 0 + 3)/3 = 3

0 3 6 30
Scheduling Algorithms 2. Shortest-Job-
First (SJF)

Shortest job served first


SJF is provably optimal when serving the short jobs first
, wait times of short job decreases more than it increases
the wait times of long processes
Good for long-term scheduling where processing times
can be estimated
Not good for short-term scheduling where processing
times cannot be estimated
Two schemes:
nonpreemptive CPU completes the current job
preemptive a new process with a lesser CPU burst is
executed before the current job (SRTF)
Scheduling Algorithms 2. Shortest Job
First (SJF) - Examples: SJF & SRTF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
non-preemptive SJF avg wait time = (0 + 6 + 3 + 7)/4 = 4

P1 P3 P2 P4

0 3 7 8 12 16

preemptive SJF (SRTF) avg wait time = (9+ 1+ 0+2)/4 = 3

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16
Scheduling Algorithms 2. Shortest Job
First (SJF) - Estimating CPU Burst

Exponential average of measured lengths of previous CPU


bursts
Define : n 1 t n 1 n .
Where
t n actual lenght of n thCPU burst
n 1 predicted value for the next CPU burst
, 0 1 is a weighting factor
If we expand the formula, we get:
n+1 = tn + (1 - ) tn-1 + + (1 - )j tn-j + + (1 - )n+10

If =0 => n+1 = n; past history only


If =1 => n+1 = tn ; recent history only
Commonly =1/2 => n+1= tn + tn-1 .. ; recent & past history
Scheduling Algorithms 2. Shortest Job First
(SJF) - Estimating CPU Burst
Scheduling Algorithms 3. Priority
Scheduling

Prioritized processes
Highest priority first
Preemptive / nonpreemptive
Determining Criteria
Internal e.g. I/O : CPU time
External e.g. fee charged, organizational rank
SJF is a priority scheduling
Criteria : Inverse of the CPU burst
Problem: Starvation low priorities may never be
executed.
Solution: Aging time variant priority
Example
Scheduling Algorithms 4. Round Robin
(RR)

Time sharing scheme time sliced (10 to 100 ms time quantum)


If CPU burst > time quantum, process is preempted and circular
queued.
Performance: n processes in ready queue and time quantum =
q, then
each process gets 1/n of the CPU time
No process waits more than (n-1)xq time units
e.g. RR with Time Quantum = 20 (simultaneous arrival; try an
example with different arrival times)

P1 P2 P3 P4

53 17 68 24

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162


Scheduling Algorithms 4. Round Robin
(RR) : Effects of Time Quantum Length

q very large Same as FCFS


q very small Processor sharing : n virtual processors at 1/n
speed
q must be large with respect to context switch, otherwise
overhead is too high.
Scheduling Algorithms 4. Round Robin
(RR) :Turnaround Time vs. Time Quantum

Optimal: 80%
of CPU bursts
must be
shorter than
time quantum
Scheduling Algorithms 5. Multilevel
Queue Scheduling

Ready queue prioritized to sub-queues


foreground (interactive high priority)
background (batch low priority)
Processes are permanently assigned to one queue
Scheduling Algorithms 5. Multilevel
Queue Scheduling

Queues themselves are scheduled. For example, system


processes get the highest priority. (See the figure),
foreground process has priority over background
processes

Each queue has its own scheduling algorithm e.g.


foreground RR
background FCFS

Disadvantage : Starvation
Scheduling Algorithms 6. Multilevel
Feedback Queue

A process moves among queues; prevents


starvation; aging can be implemented - (a
feedback process)

Example:
Q0 time quantum 8 ms
Q1 time quantum 16 ms
Q2 FCFS

Scheduling
A new job enters Q0 (FCFS), gets 8 ms; if
the work is not completed (CPU burst > 8
ms), preempted & moved to queue Q1.
In Q1 (FCFS), it gets 16 ms more. If the
work still incomplete, preempted and moved
to queue Q2 (FCFS).
Algorithm Evaluation

Following methods are used to evaluate algorithms.

Deterministic modeling
Take a particular predetermined workload and compare the
performances of different algorithms
Problem estimating actual times; good for repeated
processes

Queuing models
Model the system statistically; based on the statistical data
calculate different parameters
Ex. n = x W (Littles formula : =avg. arrival rate, W= avg.
wait time, n=no. of processes entering the queue in W time)
Algorithm Evaluation

Simulations
Programming a model of the computer system
Data for the model can be generated by random number
generator that generates random CPU burst, I/O bursts, arrival
times etc. according to a probability distribution
Such an estimate may not be accurate since the real systems
have a order of occurrence
To correct this problem we use an observed sequence
More costly

Implementation
The ideal method
Most expensive not only technical cost but the cost involved
in getting user feedback
Multiple-Processor Scheduling

CPU scheduling is more complex when multiple CPUs


are available.
Assumption: homogeneous processors within a uniform
memory access (UMA) multiprocessor
Load sharing: divide load among processors
Separate ready queues mismatch of work load
One ready queue assigned to a CPU when a job is
completed
Asymmetric multiprocessing one processor make all
scheduling decisions; no need for shared access to
system data structures
Symmetric multiprocessing - each processor is self-
scheduling; more complicated because of common data
among the processes
Real-Time Scheduling

Hard real-time systems required to complete a critical


task within a guaranteed amount of time
Resource reservation: process requests includes required
times. Scheduler either admits the process and guarantees
service or rejects the request
Has problems in systems with secondary or virtual
memories as they cause unavoidable and unforeseeable
delays
Soft real-time computing requires that critical processes
receive priority over others; priority inversion could occur
if a high priority process needs to access data being
modified by a low priority process

Potrebbero piacerti anche