0% found this document useful (0 votes)
21 views36 pages

CPU Scheduling

The document describes various CPU scheduling algorithms. It discusses basic concepts like multiprogramming to maximize CPU utilization. It examines the observed properties of processes, including their CPU-I/O burst cycles. It then covers evaluation criteria for scheduling algorithms like CPU utilization, throughput, waiting time, etc. Finally, it explains common scheduling algorithms like FCFS, SJF, priority scheduling, and round robin, providing examples of how each works.

Uploaded by

Aniket Paraswar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views36 pages

CPU Scheduling

The document describes various CPU scheduling algorithms. It discusses basic concepts like multiprogramming to maximize CPU utilization. It examines the observed properties of processes, including their CPU-I/O burst cycles. It then covers evaluation criteria for scheduling algorithms like CPU utilization, throughput, waiting time, etc. Finally, it explains common scheduling algorithms like FCFS, SJF, priority scheduling, and round robin, providing examples of how each works.

Uploaded by

Aniket Paraswar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

CPU Scheduling

• Describe various CPU-scheduling algorithms

• Evaluation criteria for selecting a CPU-scheduling


algorithm for a particular system
Basic Concepts
• Maximum CPU utilization obtained with
multiprogramming
• Several processes in memory (ready queue)
• When one process requests I/O, some other process
gets the CPU
• Select (schedule) a process and allocate CPU
Observed properties of Processes

• CPU–I/O Burst Cycle

• Process execution consists


of a cycle of CPU execution
and I/O wait

• Study the duration of CPU


bursts
Histogram of CPU-burst Times

Utility of CPU
scheduler

CPU bound
process

I/O bound
process

Large number of short CPU bursts and small number of long CPU
bursts
Preemptive and non preemptive

• Selects from among the processes in ready queue,


and allocates the CPU to one of them
• Queue may be ordered in various ways (not necessarily
FIFO)
• CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
• Scheduling under 1 and 4 is nonpreemptive
• All other scheduling is preemptive
Long Term Scheduler

CPU scheduler
Preemptive scheduling
Preemptive scheduling
Results in cooperative processes
Issues:
• Consider access to shared data
• Process synchronization
• Consider preemption while in kernel mode
• Updating the ready or device queue
• Preempted and running a “ps -el”
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible

• Throughput – # of processes that complete their execution


per time unit

• 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
Scheduling Algorithm Optimization Criteria

• Max CPU utilization


• Max throughput
• Min turnaround time
• Min waiting time
• Min response time

• Mostly optimize the average


• Sometimes optimize the minimum or maximum value
• Minimize max response time

• For interactive system, variance is important


• E.g. response time
• System must behave in predictable way
Scheduling algorithms
• First-Come, First-Served (FCFS) Scheduling

• Shortest-Job-First (SJF) Scheduling

• Priority Scheduling

• Round Robin (RR)


First-Come, First-Served (FCFS) Scheduling

• Process that requests CPU first, is allocated the CPU first


• Ready queue=>FIFO queue
• Non preemptive
• Simple to implement

Performance evaluation
• Ideally many processes with several CPU and I/O bursts

• Here we consider only one CPU burst per process


First-Come, First-Served (FCFS) Scheduling

Process Burst Time


P1 24
P2 3
P3 3
• 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
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
• The Gantt chart for the schedule is:

P2 P3 P1

0 3 6 30

• Waiting time for P1 = 6; P2 = 0; P3 = 3


• Average waiting time: (6 + 0 + 3)/3 = 3
• Much better than previous case
• Average waiting time under FCFS heavily depends on process arrival time and
burst time
• Convoy effect - short process behind long process
• Consider one CPU-bound and many I/O-bound processes
Shortest-Job-First (SJF) Scheduling
• Associate with each process the length of its next
CPU burst
• Allocate CPU to a process with the smallest next CPU
burst.
• Not on the total CPU time
• Tie=>FCFS
Example of SJF
ProcessArriva l TimeBurst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
• SJF scheduling chart
P4 P1 P3 P2

0 3 9 16 24

• Average waiting time = (3 + 16 + 9 + 0) / 4 = 7


Avg waiting time for FCFS?
SJF
• SJF is optimal – gives minimum average waiting time for a given set of
processes (Proof: home work!)
• The difficulty is knowing the length of the next CPU request
• Useful for Long term scheduler
• Batch system
• Could ask the user to estimate
• Too low value may result in “time-limit-exceeded error”
Preemptive version
Shortest-remaining-time-first

• Preemptive version called shortest-remaining-time-first


• Concepts of varying arrival times and preemption to the analysis
ProcessA arri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

• Preemptive SJF Gantt Chart


P1 P2 P4 P1 P3

0 1 5 10 17 26

Avg waiting
• Average time
waiting time for non preemptive?
= [(10-1)+(1-1)+(17-2)+(5-3)]/4 = 26/4 = 6.5 msec
Determining Length of Next CPU Burst
• Estimation of the CPU burst length – should be similar
to the previous burst
• Then pick process with shortest predicted next CPU burst
• Estimation can be done by using the length of previous
CPU bursts, using time series analysis
1. t n  actual length of n th CPU burst
2.  n 1  predicted value for the next CPU burst
3.  , 0    1
4. Define : Boundary
 n 1   tn  1    n . cases α=0, 1

• Commonly, α set to ½
Examples of Exponential Averaging
•  =0
• n+1 = n
• Recent burst time 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

• Since both  and (1 - ) are less than or equal to 1, each


successive term has less weight than its predecessor
Prediction of the Length of the
Next CPU Burst

0 1 2 3 6
Priority Scheduling
• A priority number (integer) is associated with each process

• The CPU is allocated to the process with the highest priority (smallest
integer  highest priority) nice
• Set priority value
• Internal (time limit, memory req., ratio of I/O Vs CPU burst)
• External (importance, fund etc)

• SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time

• Two types
• Preemptive
• Nonpreemptive

• Problem  Starvation – low priority processes may never execute

• Solution  Aging – as time progresses increase the priority of the process


Example of Priority Scheduling

ProcessA arri Burst TimeT Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
• Priority scheduling Gantt Chart
P2 P5 P1 P3 P4

0 1 6 16 18 19

• Average waiting time = 8.2 msec


Round Robin (RR)
• Designed for time sharing system
• 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.
• Implementation
• Ready queue as FIFO queue
• CPU scheduler picks the first process from the ready queue
• Sets the timer for 1 time quantum
• Invokes despatcher
• If CPU burst time < quantum
• Process releases CPU
• Else Interrupt
• Context switch
• Add the process at the tail of the ready queue
• Select the front process of the ready queue and allocate CPU
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

• Avg waiting time = ((10-4)+4+7)/3=5.66


Round Robin (RR)
• Each process has a time quantum T allotted to it
• Dispatcher starts process P0, loads a external counter (timer) with
counts to count down from T to 0
• When the timer expires, the CPU is interrupted
• The context switch ISR gets invoked
• The context switch saves the context of P0
• PCB of P0 tells where to save
• The scheduler selects P1 from ready queue
• The PCB of P1 tells where the old state, if any, is saved
• The dispatcher loads the context of P1
• The dispatcher reloads the counter (timer) with T
• The ISR returns, restarting P1 (since P1’s PC is now loaded as part of the
new context loaded)
• P starts running
Round Robin (RR)
• 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 depends on time quantum q
• q large  FIFO
• q small  Processor sharing (n processes has own CPU
running at 1/n speed)
Effect of Time Quantum and Context Switch Time
Performance of RR scheduling

• No overhead
• However, poor
response time

• Too much
overhead!
• q must be large with respect to context switch,
• Slowing the
otherwise overhead is too high
execution time
• q usually 10ms to 100ms, context switch < 10 microsec
Effect on Turnaround Time
• TT depends on the time quantum and CPU burst time
• Better if most processes complete there next CPU burst in a
single q
• Large q=>
processes in ready
queue suffer
• Small q=>
Completion will
take more time

80% of CPU bursts


should be shorter than
q

Response time Typically, higher average turnaround than SJF,


but better response time
Turnaround Time
q=1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
P1 P2 P3 P4 P1 P2 P4 P1 P2 P4 P1 P4 P1 P4 P1 P4 P4
`

Avg Turnaround time=


(15+9+3+17)/4=11

0 6 9 10 16 17
P1 P2 P3 P4 P4

(6+9+10+17)/4=10.5
q=6
Process classification
• Foreground process
• Interactive
• Frequent I/O request
• Requires low response time
• Background Process
• Less interactive
• Like batch process
• Allows high response time
• Can use different scheduling algorithms for two types
of processes ?
Multilevel Queue
• Ready queue is partitioned into separate queues, eg:
• foreground (interactive)
• background (batch)
• Process permanently assigned in a given queue
• Based on process type, priority, memory req.

• Each queue has its own scheduling algorithm:


• foreground – RR
• background – FCFS

• Scheduling must be done between the queues:


• Fixed priority scheduling; (i.e., serve all from foreground then
from background).
• Possibility of starvation.
Multilevel Queue Scheduling
• No process in batch queue
could run unless upper
queues are empty

• If new process enters


• Preempt

Another possibility
• Time slice – each queue gets a certain amount of CPU time which it can schedule
amongst its processes; i.e., 80% to foreground in RR
• 20% to background in FCFS
Multilevel Feedback Queue
• So a process is permanently assigned a queue when
they enter in the system
• They do not move

• Flexibility!
• Multilevel-feedback-queue scheduling
• A process can move between the various queues;
• Separate processes based of the CPU bursts
• Process using too much CPU time can be moved to lower
priority
• Interactive process => Higher priority
• Move process from low to high priority
• Implement aging
Example of Multilevel Feedback Queue
Q0
• Three queues:
• Q0 – RR with time quantum 8 milliseconds
Q1
• Q1 – RR time quantum 16 milliseconds
• Q2 – FCFS
Q2
• Scheduling
• A new job enters queue Q0
• 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 receives 16 milliseconds
• If it still does not complete, it is preempted and moved to
queue Q2
Multilevel Feedback Queues

• Highest Priority to processes


CPU burst time <8 ms
• Then processes >8 and <24
• Multilevel-feedback-queue scheduler defined by the following
parameters:
• number of queues
• scheduling algorithms for each queue
• method used to determine when to upgrade a process
• method used to determine when to demote a process
• method used to determine which queue a process will enter when that
process needs service

You might also like