OS Chapter 4
OS Chapter 4
CPU Scheduling
04/28/2025 1
Outline
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
Multiple-Processor Scheduling
Real-Time CPU Scheduling
Algorithm Evaluation
04/28/2025 2
Cpu scheduling
CPU scheduling is a process used by the operating
system to decide which task or process gets to use
the CPU at a particular time. This is important
because a CPU can only handle one task at a time,
but there are usually many tasks that need to be
processed. The following are different purposes of
a CPU scheduling time.
– Maximize the CPU utilization
– Minimize the response and waiting time of the process.
04/28/2025 3
• Maximum CPU utilization obtained with multi
programming.
• CPU-I/O Burst Cycle - Process execution
consists of a cycle of CPU execution and I/O
wait.
• Short-term scheduler -selects from among the
processes in memory that are ready to
execute, and allocates the CPU to one of them
04/28/2025 4
Cont..
• 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 non-preemptive.
• All other scheduling is preemptive
04/28/2025 5
Dispatcher
• Dispatcher module gives control of the CPU to the
process selected by the short term scheduler; this
involves:
– switching context
– switching to user mode
– jumping to the proper location in the user program to
restart that program means Transfers control to the
next process so it can start executing.
• Dispatch latency - time it takes for the dispatcher
to stop one process and start another running.
04/28/2025 6
Scheduling Criteria
• CPU utilization - keep the CPU as busy as possible
• Throughput – the number of processes that complete their
execution per time unit.
• Turnaround time - total amount of time from when a process is
submitted to ready queue until it completes execution.
• 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)
• Optimization
– Max CPU utilization
– Max throughput
– Minimum turnaround time
– Minimum waiting time
– Minimum response time
04/28/2025 7
Cpu Scheduling algorithms and types
These algorithms are broadly classified into preemptive and non-preemptive types
_Preemptive Scheduling:
In preemptive scheduling, the CPU can interrupt a running process to allocate resources to a
higher-priority process or another task. Algorithms such as Round Robin, and Shortest
Algorithms like First Come First Serve (FCFS), Shortest Job First (SJF) are an example
04/28/2025 8
Cpu Scheduling algorithms
• First-Come, First-Served (FCFS) Scheduling
• Shortest-Job-First (SJF) Scheduling
• Priority Scheduling
• Round Robin (RR)
• Multilevel Queue
• Multilevel Feedback Queue
04/28/2025 9
Terms are used while solving cpu scheduling
algorithms.
• Arrival time (AT) − Arrival time is the time at which the process arrives in ready
queue.
• Burst time (BT) or CPU time of the process − Burst time is the unit of time in
which a particular process completes its execution.
• Completion time (CT) − Completion time is the time at which the process has been
terminated.
• Turn-around time (TAT) − The total time from arrival time to completion time is
known as turn-around time.
Completion time (CT) − Arrival time (AT) or TAT = Burst time
(BT) + Waiting time (WT)
• Waiting time (WT) Waiting time is the time at which the process waits for its
allocation while the previous process is in the CPU for execution. WT is written as,
Waiting time (WT) = Turn-around time (TAT) − Burst time (BT)
• Response time (RT) − Response time is the time at which CPU has been allocated to
a particular process first time.
• In04/28/2025
case of non-preemptive scheduling, generally Waiting time and Response time is 10
First-Come, First-Served (FCFS)
Scheduling
• The process that arrives in the ready queue
first is allocated to execute in the CPU first.
• FCFS is a non-preemptive scheduling
algorithm.
• Implementation is easy since it does not
involve any complex algorithm.
04/28/2025 11
First-Come, First-Served (FCFS) Scheduling
04/28/2025 12
solution
p3 p4 p1 p2 p5
0 4 11 13 19 23
04/28/2025 13
Activity 1
04/28/2025 14
Cont…
In FCFS algorithm,if a process with higher burst time comes first in
the ready queue then the processes with lower burst time may get
blocked and that processes with lower burst time may not be able
to get the CPU if the higher burst time task takes time forever.
If a process with long burst time comes in the line first then the
other short burst time process have to wait for a long time, so it is
not much good as time-sharing systems.
Since it is non-preemptive, it does not release the CPU before it
completes its task execution completely. Prove the 2nd theory by
considering different examples.
04/28/2025 15
Shortest-Job-First (SJF) Scheduling
• In the Shortest Job First scheduling algorithm, the processes are
scheduled in ascending order of their CPU burst times.
• Two schemes:
a) non-preemptive - once CPU given to the process it cannot be
preempted until it completes its CPU burst.
b) preemptive – it also refers Shortest Remaining Time First
(SRTF) scheduling algorithm. if a short process enters the ready
queue while a longer process is executing, process switch occurs
by which the executing process is swapped out to the ready
queue while the newly arrived shorter process starts to execute.
In cases where two or more processes have the same burst
time, arbitration is done among these processes on first come
first serve basis.
• SJF is optimal - gives minimum average waiting time for a given
set of processes.
04/28/2025 16
Example for non preemption sjf
04/28/2025 17
Solution by non preemption sjf
Consider all processes arrived at t=0
04/28/2025 18
Preemption sjf example
04/28/2025 19
Gant chart
04/28/2025 20
Exercise(SJF)
• Compute using both preemption and non
preemption sjf.
ProcessA arri ATT BT CT
WT TAT
P1 0 8
P2 1 4
P3 2 9
P4 3 5
04/28/2025 21
Priority Scheduling
• In priority scheduling, the process with the highest priority is assigned to
the CPU.
• A priority number (integer) is associated with each process.
• Some systems follow the scheme that lower the priority value, higher
the priority; while other systems follow the scheme of higher the
priority value, higher the priority.
• If two processes are of same highest priority, then the scheduler
arbitrates between them on first come first serve basis.
• There are 2 types of Priority Scheduling
a) Preemptive Priority Scheduling
b) non-preemptive Priority Scheduling.
• Problem = Starvation (or indefinite blocking) - low priority processes may never execute.
• Solution = Aging - as time progresses increase the priority of the process
04/28/2025 22
Non-preemptive Priority Scheduling
• In the non-preemptive version, once a process is
assigned to the CPU, it runs into completion.
Example:-
04/28/2025 23
solution
04/28/2025 24
Preemptive Priority Scheduling
• In preemptive priority scheduling, if a process
arrives that has higher priority than the executing
process, then the higher priority process pre-empts
the lower priority process. example:
04/28/2025 25
solution
04/28/2025 26
Round Robin (RR)
In RR scheduling, each process gets equal time slices (or time
quanta) for which it executes in the CPU in turn wise manner.
• Round Robin scheduling is preemptive.
• any new process that arrives the system is inserted at the end
of the ready queue in FCFS manner.
• If04/28/2025
the required burst time is more than the time quantum,27the
Let us consider time quantum of 2ms or Q=2ms and perform RR scheduling on this. We
will draw GANTT chart and find the average turnaround time and average waiting time.
Solution:
04/28/2025 28
04/28/2025 29
Multilevel Queue algorithm
• Processes are grouped into several queues.
• Example:
foreground (interactive) may assigned to q1
background (batch) may assigned to q2
• Process cannot move between the queues.
• Each queue possesses a different priority, and process type.
• The scheduling algorithm can be different for each queue and these
processes are mapped in a permanent manner to a particular
queue.
• Each queue has its own scheduling algorithm.
• Example:
foreground - RR
background - FCFS
04/28/2025 30
Features of Multilevel Queue (MLQ) CPU Scheduling
04/28/2025 32
Consider the below table of four processes under Multilevel queue scheduling. Queue
number denotes the queue of the process.
Priority of queue 1 is greater than queue 2. queue 1 uses Round Robin (Time Quantum = 2)
and queue 2 uses FCFS.
Solution
04/28/2025 33
• At starting, both queues have process so process in
queue 1 (P1, P2) runs first (because of higher priority)
in the round robin fashion and completes after 7 units
• Then process in queue 2 (P3) starts running (as there
is no process in queue 1) but while it is running P4
comes in queue 1 and interrupts P3 and start running
for 5 seconds and
• After its completion P3 takes the CPU and completes
its execution.
04/28/2025 34
Multilevel Feedback Queue algorithm
• A process can move between the various queues; aging can
be implemented this way.
• features of Multilevel Feedback Queue:-
– Priorities adjusted dynamically:- process can move between
different priority queues.
– Multiple queues:- Schedulers dévides processes into multiple
queues.
– Time-slicing: Each queue is assigned a time quantum or time
slice.
– Preemption: Preemption: Preemption is allowed in MLFQ
scheduling, meaning that a higher-priority process can preempt
a lower-priority process to ensure it gets the CPU time it needs.
04/28/2025 35
Example of multilevel feedback queue
• Three queues:
– Q0 - time quantum 8 milliseconds
– Q1 - time quantum 16 milliseconds
– Q2 - FCFS
Scheduling
• A new job enters queue Q0 which is served RR. 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 ,job is again served
FCFS.
04/28/2025 36
Example :-Assume three processes arrive at time t=0 and three queues(q0,q1,and q2) are
required for the processes. the priority of queues are ascending from q2,q1, and q0)
38
04/28/2025
Real-Time Scheduling
• Real-time scheduling is a specialized CPU
scheduling approach used in systems that
require tasks to be completed within strict time
constraints. Types of Real-Time Systems:-
• Hard real-time systems - required to complete a
critical task within a guaranteed amount of time.
• Soft real-time system- requires that critical
processes receive priority over less fortunate
ones.
04/28/2025 39
Algorithm Evaluation
• Deterministic modeling - takes a particular
predetermined workload and defines the
performance of each algorithm for that workload.
• Queuing models - make a mathematical model
based on the distributions of job start times and
burst times.
• Simulation - write a program to schedule imaginary
tasks using various algorithms.
• Implementation - code the algorithms into the OS.
04/28/2025 40
Su m m ary
04/28/2025
? 42