OS UNIT 2 CPU Scheduling
OS UNIT 2 CPU Scheduling
By
Amandeep Kaur
TOPICS
• Types of Schedulers
• Scheduling Algorithms (Pre-emptive and Non-Preemptive..NP) & Criteria
• Dispatcher
• First Come First Serve (FCFS), Shortest Job First (SJF) under NP
• Round Robin (RR), Priority Scheduling (PS), Multi Level Feedback Queue (MLFQ)
under Pre-emptive
• Multiprocessor Scheduling (MS)
• Real Time Scheduling (RTS)
• Thread Scheduling (TS)
Why
• To maximize CPU utilization.
• Context Switching
• The aim of processor scheduling is to assign
Scheduli processes to be executed by the processor.
• Scheduling affects the performance of the
its types
• Long-Term Scheduling or Job
Scheduler. i.e. Ready Queue
• Medium-Term Scheduling or
Swapping Scheduler i.e. Waiting
Queue
• Short-Term Scheduling or CPU
Scheduler. i.e. Running Queue
TYPES OF SCHEDULERS
LTS,STS,MTS
Which of the following best describes the function of
short-term scheduling (STS) in an operating system?
A) Selects which process should be brought into the ready queue from the job pool.
B) Selects which process should be swapped out of the main memory to the
secondary storage.
C) Selects which process should be executed next by the CPU from the ready
queue.
D) Determines which process should be terminated to free up resources.
ANS: C
C) Selects which process should be executed next by the CPU from the ready
queue.
A. CPU utilization
B. Throughput
C. Turn Around Time
D. Waiting time
ANS. C
C. Turnaround time
• Scheduling algorithms or scheduling policies are
mainly used for short-term scheduling and the main
objective of short-term scheduling is to allocate
Schedulin
processor time in such a way as to optimize one or
more aspects of system behavior.
g
• For these scheduling algorithms assume only a single
processor is present.
• Scheduling algorithms decide which of the processes in
Algorithm the ready queue is to be allocated to the CPU is basis
on the type of scheduling policy and whether that
s policy is either preemptive or non-preemptive. For
scheduling arrival time and service time are also will
play a role.
NP( • FCFS (First-come First Served)
NON • SJF (Shortest Job First )
-
PRE
MPTI
Schedulin
VE)
• SRTF (Shortest Remaining Time First)
• PRIORITY
g PRE
EMP
TIVE
•
•
RR (Round-Robin)
MLFQ (Multilevel Feedback Queue)
Algorithm
s
• MULTIPROCESSOR
• RT (Real Time )
• THREAD
MIX
GANTT CHART (GC)
• It is simply used for
graphical representation
of schedule .
• A Gantt chart is a
horizontal bar chart used
to represent operating
systems CPU scheduling in
graphical view that help
to plan, coordinate and
track specific CPU utilization
factor like throughput,
waiting time, turnaround
time etc.
FIRST COME FIRST SERVED
FCFS: SCHEDULING (FCFS)
• The process which arrives first in the
Disadv.
ready queue is firstly assigned the CPU.
• In case of a tie, process with smaller Convey Effect:
process id is executed first. • Consider processes with higher burst time
• It is always non-preemptive in nature. arrived before the processes with smaller
burst time.
• Then, smaller processes have to wait for a
Adv. long time for longer processes to release
• It is simple and easy to understand. the CPU.
• It can be easily implemented using
queue data structure.
• It does not lead to starvation.
ROLE PLAY (SRTF)
EXERCISE (FCFS)
ANS. AVG. TAT=29/5 AND
AVG. WT=16/5
SHORTEST JOB FIRST(SJF)
• Out of all the available processes, CPU is
assigned to the process having smallest
burst time.
• In case of a tie, it is broken by
FCFS Scheduling. SR
TF
• It is Non-Preemptive (NP) in nature.
P ) EM E ( P
(N PT - R
J F ) IVE
S
SHORTEST REMAINING TIME
FIRST(SRTF)
• SJF Scheduling can be used in both Disadv.
preemptive and non-preemptive mode. • It can not be implemented practically
• Preemptive mode of Shortest Job First is since burst time of the processes can
called as Shortest Remaining Time not be known in advance.
First (SRTF). • It leads to starvation for processes with
larger burst time.
Adv. • Priorities can not be set for the
• SRTF is optimal and guarantees the processes.
minimum average waiting time. • Processes with larger burst time have
• It provides a standard for other poor response time.
algorithms since no other algorithm
performs better than it.
EXAMPLE (SJF)
ANS. AVG. TAT=40 / 5 = 8 UNIT
AND AVG. WT= 24 / 5 = 4.8 UNIT
EXAMPLE(SRTF)
ANS. AVG. TAT=35 / 5 = 7 UNIT
AND AVG. WT= 19 / 5 = 3.8 UNIT
In the context of CPU scheduling, the shortest job
first (SJF) algorithm is optimal in terms of:
B) In SRTF, the process with the longest remaining time is given the CPU next.
D) SRTF can lead to a convoy effect, where longer processes are delayed indefinitely.
ANS. C
C) SRTF is an optimal scheduling algorithm for minimizing average waiting time.
PRIORITY SCHEDULING
• Out of all the available processes, CPU Adv:
is assigned to the process having the • It considers the priority of the
highest priority. processes and allows the important
• In case of a tie, it is broken by processes to run first.
FCFS Scheduling. • Priority scheduling in preemptive mode
• Priority Scheduling can be used in both is best suited for real time operating
preemptive and non-preemptive mode. system.
• By default, lower the number means
higher the priority otherwise if Disadv.
mentioned in the question then follow
the higher number means higher • Processes with lesser priority may
priority. starve for CPU.
• There is no idea of response time and
waiting time.
Note 1:
• The waiting time for the process having the
highest priority will always be zero in
Importan
preemptive mode.
• The waiting time for the process having the
highest priority may not be zero in non-
t Notes-
preemptive mode.
Note 2:
• Priority scheduling in preemptive and non-
preemptive mode behaves exactly same under
following conditions-
• The arrival time of all the processes is same
EXAMPLE(PRIORITY)
ANS. AVG. TAT= 41 / 5 = 8.2 UNIT
AND AVG. WT= 26 / 5 = 5.2 UNIT
EXAMPLE (PRIORITY)
ANS. AVG. TAT= 38 / 5 = 7.6 UNIT
AND AVG. WT= 23 / 5 = 4.6 UNIT
Which of the following CPU scheduling algorithms
can cause the phenomenon known as "starvation"?
• Note-02:
• Note-04:
• With increasing value of time quantum,
• Number of context switch decreases
• The performance of Round Robin scheduling
• Response time increases
heavily depends on the value of time
• Chances of starvation increases quantum.
• The value of time quantum should be such
• Thus, higher value of time quantum is better in that it is neither too big nor too small.
EXAMPLE(RR)
ANS. AVG. TAT=43 / 5 = 8.6 UNIT AND
AVG. WT= 29 / 5 = 5.8 UNIT
ANS. AVG. TAT=43 / 5 = 8.6 UNIT AND
AVG. WT= 29 / 5 = 5.8 UNIT
ANS. AVG. TAT=43 / 5 = 8.6 UNIT AND
AVG. WT= 29 / 5 = 5.8 UNIT
In the round robin scheduling algorithm, what primarily
determines the time a process is allowed to run before it is
swapped out?
• Real time scheduling is of two types: • The foremost is that the processes are
• Soft Real-Time scheduling which considered periodic i.e., the process
does not guarantee when when a will repeat itself after a fixed period of
critical real-time process will be time. The period of a process is
scheduled.(Algorithm: Rate Monotonic) denoted by p. The next characteristic is
the processing time t i.e., the time for
• Hard Real-Time scheduling in which which the process requires the CPU
the process must be scheduled before within each period. In other words
the deadline.(Algorithm: Earliest Dead processing time refers to the burst
Line(EDL)) time. The final characteristic is the
deadline d, i.e., the time before which
the process must be serviced.
TYPES OF RT SCHEDULING
A) Predictability
B) Deadline adherence
C) Preemptive scheduling
D) Round-robin fairness
ANS.D
D) Round-robin fairness
Explanation: Real-time scheduling focuses on predictability, deadline adherence,
and preemptive scheduling to ensure critical tasks meet their deadlines. Round-
robin fairness is not a priority in real-time systems, as it might not guarantee that
high-priority or time-sensitive tasks are completed on time.
• In multi-processor system, there are multiple CPUs.
• Load Balancing: The load is distributed among these
available processors.
• But in multi-processor system, it is quite complex.
Multi-
Types:
Processor • Asymmetric Multiprocessing: All scheduling decisions,
Scheduling I/O processing, and other system activities handled by a
single processor- master server.
• Local Ready Queue
• Symmetric Multiprocessing (SMP): Each processor is
self-scheduling. All processes may be in a common
ready queue, or each processor may have its own
private queue of ready processes.
• Global Ready Queue
• Avoid migration of processes from one processor to
another and instead attempt to keep a process
Process
running on the same processor. This is known as
processor affinity.
Affinity
• i.e. a process has an affinity for the processor on
which it is currently running.
and its
Types:
• Soft affinity: It is possible for a process to migrate
types
between processors.(migration possible)
• Hard affinity: Allowing a process to specify that it is
not to migrate to other processors. .(migration strictly
not possible)
• A thread is the smallest unit of execution which has its own
thread ID, program counter, register set and stack. All the
threads that belong to the same process share the code,
data section and other resources like open files belonging
to the process. So, remember thread is a part of the
Thread process. A thread can not exist without a process.
SchedulinTypes:
g • User threads are supported above the kernel and
managed without kernel support whereas Kernel
threads are supported and managed directly by OS.
REFERENCES
• OPERATING SYSTEM CONCEPTS by ABRAHAM
SILBERSCHATZ, PETER B. GALVIN, GERG GAGNE, WILEY
• DESIGN OF THE UNIX OPERATING SYSTEM by MAURICE
J. BACH, Pearson Education India
• REAL-TIME SYSTEMS by JANE W. S. LIU, Pearson
Education India
• https://www.javatpoint.com/os-scheduling-algorithms
• https://www.idconline.com/technical_references/pdfs/inf
ormation_technology/Types_of_Scheduling.pdf
REFERENCES
• https://www.gatevidyalay.com/first-come-first-serve-cp
u-scheduling/
• https://www.gatevidyalay.com/sjf-scheduling-srtf-cpu-sc
heduling/
• https://www.gatevidyalay.com/round-robin-round-robin-
scheduling-examples/
• https://www.javatpoint.com/os-fcfs-scheduling
THANK YOU