You are on page 1of 39

PROCESS SCHEDULING

PREVIOUS SESSION
1. Interprocess communication ( IPC )
2. Introduction to threads
3. Difference between threads and process
4. Multiprocessing vs multithreading
5. Multithreading challenges

• Watch my all previous OS sessions by visiting the playlist link in description


• Long session – timeline in description
• Subscribe for further updates
AGENDA
• IO Bound Process vs CPU Bound process
• Types of scheduling queues and schedulers
• Preemptive vs Nonpreemptive scheduling
• Role of Dispatcher
• Context Switch
• Scheduling criteria
• Scheduling algorithms ( FCFS, SJF, SRTF, Priority, Round Robin)
• Multilevel Queue Scheduling
INTERVIEW QUESTIONS
• What are different scheduling criterias
• Why is it important for the scheduler to distinguish I/0-bound
programs from CPU-bound programs?
• Difference between short-term, medium-term and long-term
scheduler
• What is context switch and a dispatcher?
• Difference between preemptive and nonpreemptive scheduling
• What is convoy effect in OS
• What is aging in OS?
• How to implement FCFS, SJF, SRTF, Priority or Round Robin
scheduling?
• What is starvation in process scheduling?
• Advantage and disadvantage of each process scheduling algorithm
• What is waiting time, turnaround time, response time in process
scheduling?
Need for process scheduling
• No choice
• Process terminates
• Process undergoes IO
• Multiple process waiting to run
• Faster response time
• Better CPU utlization
IO BOUND PROCESS
• Process spends most of it time waiting on IO
• Runs for a short time and the blocks on IO ( network, disk, keyboard )
• Example: Most GUI applications. Waiting for user interaction
• Text editors, terminals, process which does heavy read/write to disks
CPU BOUND PROCESS
• Most time is spent in executing code
• They run till they are preempted

While(1) {
// code
}

• Programs that does a lot of mathematical calculations like games


SOME PROCESS CAN BE BOTH
• Text Editor – waiting for input + spell check
• Games – waiting for input + render graphics

Detailed video: https://youtu.be/yKZrjI74VCk


SCHEDULERS
• Long-term scheduler
• Loads tasks from job queue ( disk ) to main memory ( Ready queue )
• Used in batch OS
• Doesn’t run very frequently
• Short-term scheduler
• Also called CPU scheduler
• Schedules process present in ready queue on CPU
• Runs very frequently – needs to be very fast
• Medium-term scheduler
• Memory manager
• Swapping in and out pages
Detailed video: https://youtu.be/ALSbJ2OodpQ
SCHEDULING QUEUES
• Job queue
• Tasks waiting on the disk
• Used in batch OS
• Long-term scheduler
• Ready queue
• Process waiting for the CPU are kept here
• PCB are stored here
• Short-term scheduler
• Device queue
• Each IO device has a waiting queue
Detailed video: https://youtu.be/xrUNRF5IWGs
CONTEXT SWITCH
CONTEXT SWITCH
• When one process is removed and another is scheduled, context
switch happens
• CPU registers value are saved to PCB of removed process
• Register values from PCB of scheduled process are loaded into CPU
registers

Detailed video: https://youtu.be/bId-MwXiDaI


DISPATCHER
• Gives control of the CPU to the process selected by short-term
scheduler
• Functions
• Context switch
• Switching to user mode
Detailed video - https://youtu.be/SydpWAOzmuE
NONPREEMPTIVE AND PREEMPTIVE
SCHEDULING
• In nonpreemptive scheduling, a process leaves the CPU voluntry
• Process terminates
• Process undergoes IO

• In preemptive scheduling, there are other reasons why process can


be kicked out from CPU
• Time slice of process expired
• A higher priority process entered the ready queue
• An interrupt ( hardware ) arrives on the CPU
Detailed video: https://youtu.be/K6N1eLbSu1o
SCHEDULING CRITERIA
• Cpu utilization – How much CPU is busy?
• Throughput – Number of processes completed per unit time
• Turnaround time(TAT) – Interval from time of submission of process
to completion of it
• Waiting time(WT) – Total time spent by a process in ready queue
• Response time(RT) – Interval from time of submission of process to
receiving of first response or time spent by process in ready queue for
the first time
OTHER IMPORTANT TERMS
• Arrival time (AT) – Time at which a process arrives in a ready queue
• Completion time (CT) – Time at which the process completes or
terminates
• Start time (ST) – Time at which a process starts its execution for the
first time
• Burst time (BT) – Total time spent on CPU or IO – CPU BT or IO BT
• By default, BT is CPU burst time

Detailed video - https://youtu.be/maosvQi-uWQ


FORMULAS
• TAT = CT – AT
• TAT = Total time spent in ready queue + total time spent on IO +. Total time
spent on the CPU
• TAT = WT + BT
• Do take care when you have IO
• WT = TAT – BT
• RT = ST – AT
• CPU Utilization = ((Total time – idle time)/total time)*100
• Throughput = Total process / Schedule length
• Schedule length = Max(CT) – Min(AT)
First come First Serve(FCFS) SCHEDULING
ALGORITHM
• The process which requests CPU first gets it first
• Schedule according to the Arrival time of the process
• FIFO implementation
• Nonpreemptive scheduling algorithm
MORE EXAMPLES FCFS FOR PRACTICE
• https://youtu.be/CHeAo9U2s_E
• https://youtu.be/_V23PCNnX9E
• https://youtu.be/pAwee32uRyo
ADVANTAGE AND DISADVANTAGE
Advantage
• Simple to implement
• No starvation

Disadvantages
• Waiting time can be very high
• Convoy effect – a process with large burst time increases waiting time for
all other processes behind it
• Favors CPU bound process over IO
• Less throughput and CPU utilization
Shortest-Job-First Scheduling (SJF)
• The process with least burst time gets CPU first
• Based on burst time
• If CPU burst time is same, use FCFS.
• Nonpreemptive scheduling
MORE EXAMPLES SJF FOR PRACTICE
• https://youtu.be/h79jrm2Jkuc
• https://youtu.be/B5gR_EC2IHM
Advantages and Disadvantages
Advantages
• Easy to implement
• Shorter jobs first – less waiting time
• Higher CPU utlilization and throughput than FCFS

Disadvantages
• Can lead to starvation
• Convoy effect possible here also
• Tough to implement ( predicting next CPU burst time )
Shortest Remaining Time First (SRTF)
• Similar to SJF
• Preemptive Scheduling
MORE EXAMPLES SRTF FOR PRACTICE
• https://youtu.be/6iG_c8I97rI
• https://youtu.be/kH4Uvr6qbTI
Advantages and Disadvantages
Advantages
• Shorter jobs first – less waiting time than FCFS and SJF
• Higher CPU utlilization and throughput than FCFS and SJF
• No convoy effect

Disadvantages
• Can lead to starvation
• Tough to implement ( predicting next CPU burst time )
Nonpreemptive priority scheduling
• Similar to SJF
• Here priority is provided explicitly
• In SJF, priority -> Burst time
• Priority -> 0-127
• Which is high priority and which is low?
• Nonpreemptive scheduling
• In case priority are same, follow FCFS
MORE EXAMPLES NONPREEMPTIVE PRIORITY
FOR PRACTICE
• https://youtu.be/8MCb5S_iSjU
• https://youtu.be/3MXQ4uP0XsA
• https://youtu.be/3lGhsUYMPDc
Preemptive Priority Scheduling
• Similar to SRTF
• Priority is explicitly mentioned here
• Preemptive scheduling
MORE EXAMPLES PREEMPTIVE PRIORITY FOR
PRACTICE
• https://youtu.be/VbFyvHSxftI
• https://youtu.be/SkpB0NDYS8E
Advantages and disadvantages
Advantage
• Easy to implement
• Good for critical process

Disadvantage
• Suffers from starvation (solved using aging technique )
• Deciding priority of the process
AGING TECHNIQUE
• A priority scheduling algorithm can leave some lowpriority processes
waiting indefinitely or starved
• Rumor has it that when they shut down the IBM 7094 at MIT in 1973,
they found a low-priority process that had been submitted in 1967
and had not yet been run.
• Aging is a technique of gradually increasing the priority of processes
that wait in the system for a long time. For example, if priorities range
from 127 (low) to 0 (high), we could decrease the priority of a waiting
process by 1 every 15 minutes.
ROUND ROBIN (RR) Scheduling algorithm
• Time sharing systems
• FCFS + preemption
• Time quantum or Time slice ( 1 to 100 ms )
• Implemented using FIFO queue
RR MORE EXAMPLES FOR PRACTICE
• https://youtu.be/kfYXMbc-Tok
• https://youtu.be/1d8r3M_tVXw
Advantages and disadvantages
Advantage
• Better response time
• No starvation
• No convoy effect

Disadvantages
• Average waiting time very high
• Time slice is low -> More context switches
• Time slice is high -> Becomes more like FCFS
MULTILEVEL QUEUE SCHEDULING
• Ready queue is divided into multiple queues
• Each queue has its own scheduling algorithm
• Scheduling algorithms among queues ( priority )
• Each queue has absolute priority over lower-priority queues. No process in
the batch queue, for example, could run unless the queues for system
processes, interactive processes, and interactive editing processes were all
empty. If an interactive editing process entered the ready queue while a
batch process was running, the batch process would be preempted.
• Another possibility is to time-slice among the queues. Here, each queue
gets a certain portion of the CPU time, which it can then schedule among
its various processes. For instance, in the foreground-background queue
example, the foreground queue can be given 80 percent of the CPU time
for RR scheduling among its processes, whereas the background queue
receives 20 percent of the CPU to give to its processes on an FCFS basis.
Multilevel Feedback Queue Scheduling
• In multilevel queue scheduling, processes are permanently assigned to a
queue
• This algorithm allows processes to move between queues.
• The idea is to separate processes according to the characteristics of their
CPU bursts.
• If a process uses too much CPU time, it will be moved to a lower-priority
queue. This scheme leaves I/O-bound and interactive processes in the
higher-priority queues.
• In addition, a process that waits too long in a lower-priority queue may be
moved to a higher-priority queue. This form of aging prevents starvation.
SOLUTIONS TO GIVEN PROBLEMS
• FCFS Example -> Question 1
• SJF AND SRTF -> Question 4
• Priority scheduling -> Question 7
• Round Robin -> Question 9

Link to questions and answers -


https://www.scribd.com/doc/121618986/Process-Scheduling-
problems

You might also like