Lecture 5 - Scheduling
Lecture 5 - Scheduling
Bilal Ahmed
1
CPU Scheduling
• CPU scheduling is the process of determining which process in the ready queue
is allocated to the CPU.
• Various scheduling algorithms can be used to make this decision, such as
• First- Come-First-Served (FCFS)
• Shortest Job Next (SJN),
• Priority
• Round Robin (RR)
• Different algorithm support different class of process and favor different
scheduling criterion.
Type of scheduling
Non-Pre-emptive: Under Non-Pre-emptive scheduling, once the CPU has been
allocated to a process, the process keeps the CPU until it releases the CPU
willingly.
A process will leave the CPU only
• When a process completes its execution (Termination state)
• When a process wants to perform some i/o operations(Blocked state)
Pre-emptive
• Pre-emptive scheduling: Once the CPU has been allocated to a process, A process
will leave the CPU willingly or it can be forced out. So it will leave the CPU
• When a process completes its execution
• When a process leaves CPU voluntarily to perform some i/o operations
• If a new process enters in the ready states (new, waiting), in case of high
priority
• When process switches from running to ready state because of time
quantum expire.
Point Non-Pre-emptive Scheduling Pre-emptive Scheduling
Note: The CPU-scheduling algorithm does not affect the amount of time
during which a process executes or perform I/0; it affects only the
amount of time that a process spends waiting in the ready queue.
Terminology
• Arrival Time (AT): Time at which process enters a ready state.
• Burst Time (BT): Amount of CPU time required by the process to finish its execution.
• Turn Around Time (TAT): Completion Time (CT) – Arrival Time (AT), Waiting Time + Burst Time
(BT)
Bilal Ahmed
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 2 4
P1 1 2
P2 0 3
P3 4 2
P4 3 1
Average
Bilal Ahmed
Advantage
• Easy to understand, and can easily be implemented using Queue data
structure.
P0 0 100
P1 1 2
Average
P0 1 100
P1 0 2
Average
Bilal Ahmed
Convoy Effect
• If the smaller process have to wait more for the CPU because of Larger process then this
effect is called Convoy Effect, it result into more average waiting time.
• Solution, smaller process have to be executed before longer process, to achieve less average
• waiting time.
Disadvantage
FCFS suffers from convoy which means smaller process have to wait larger process,
which result into large average waiting time.
• The FCFS algorithm is thus particularly troublesome for time-sharing systems (due to
its non-pre-emptive nature), where it is important that each user get a share of the
CPU at regular intervals.
Bilal Ahmed
Shortest Job First (SJF)(non-pre-emptive)
Bilal Ahmed
Shortest Remaining Time First (SRTF)/ (Shortest Next CPU Burst) (Pre-emptive)
P1 2 5
P2 3 1
P3 4 2
P4 5 8
Average
Non Pre-emptive
Pre-emptive
Bilal Ahmed
Advantage
• Pre-emptive version guarantees minimal average
waiting time so some time also referred as optimal
algorithm. Provide a standard for other algo in
terms of average waiting time. Provide better
average response time compare to FCFS.
Disadvantage
• Here process with the longer CPU burst
requirement goes into starvation and have
response time.
• This algo cannot be implemented
• As there is no way to know the length of the next
CPU burst. Bilal Ahmed
Priority scheduling
Priority scheduling
• Here a priority is associated with each process. At any instance of time out of all available
process, CPU is allocated to the process which possess highest priority (may be higher or
lower number).
• Tie is broken using FCFS order. No importance to senior or burst time. It supports both
non-pre- emptive and pre-emptive versions.
• In Priority (non-pre-emptive) once a decision is made and among the available
process, the process with the highest priority is scheduled on the CPU, it cannot be
pre-empted even if a new process with higher priority more than the priority of the
running process enter in the system.
• In Priority (pre-emptive) once a decision is made and among the available process, the
process with the highest priority is scheduled on the CPU. if it a new process with
priority more than the priority of the running process enter in the system, then we do a
context switch and the processor is provided to the new process with higher priority.
• There is no general agreement on whether 0 is the highest or lowest priority, it can vary
from systems to systems.
P. No AT BT Priority CT TAT = CT - AT WT = TAT - BT
P0 1 4 4
P1 2 2 5
P2 2 3 7
P3 3 5 8(H)
P4 3 1 5
P5 4 2 6
Average
Non Pre-emptive
Pre-emptive
Bilal Ahmed
Advantage
Gives a facility specially to system process.
Allow us to run important process even if it is a user process.
Disadvantage
Here process with the smaller priority may starve for the CPU
No idea of response time or waiting time.
Suppose T q=2
Bilal Ahmed
Advantage
• Perform best in terms of average response time
• Works will in case of time-sharing systems, client server architecture and interactive
system
• kind of SJF implementation
Disadvantage
• Longer process may starve
• Performance depends heavily on time quantum - If value of the time quantum is very less,
then it will give lesser average response time (good but total no of context switches will be
more, so CPU utilization will be less), If time quantum is very large then average response
time will be more bad, but no of context switches will be less, so CPU utilization will be
good.
• No idea of priority
So which one is best?
• FCFS
• SJF
• Priority
• Round Robin
Multi Level-Queue Scheduling
• After studying all important approach to CPU scheduling, we must understand anyone of them
alone is not good for every process in the system, as different process have different
scheduling needs so, we must have a kind of hybrid scheduling idea, supporting all classes of
processes.
• A multilevel queue scheduling algorithm, partitions the ready queue into several separate
queues. The processes are permanently assigned to one queue, generally based on properties
and requirement of the process.
• Each queue has its own scheduling algorithm. For example
• System process might need priority algorithm
• Interactive process might be scheduled by an RR algorithm
• Batch processes is scheduled by an FCFS algorithm.
• In addition, there must be scheduling among the queues, which is commonly implemented as
fixed-priority preemptive scheduling or round robin with different time quantum.
Bilal Ahmed
Multi-level Feedback Queue Scheduling
• Problem with multi-level queue scheduling is how to decide number of ready
queue, scheduling algorithm inside the queue and between the queue and
once a process enters a specific queue we can not change and queue after
that.
• The multilevel feedback queue scheduling algorithm, allows a process 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. 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.
• A process entering the ready queue is put in queue 0. A process in queue 0 is
given a time quantum of 8 milliseconds. If it does not finish within this time, it is
moved to the tail of queue 1. If queue 0 is empty, the process at the head of
queue 1 is given a quantum of 16 milliseconds. If it does not complete, it is
preempted and is put into queue 2. Processes in queue 2 are run on an FCFS
basis but are run only when queues 0 and 1 are empty.
• In general, a multilevel feedback queue scheduler is defined by
the following parameters:
• The number of queues
• The scheduling algorithm for each queue
• The method used to determine when to upgrade a process
to a higher- priority queue
• The method used to determine when to demote a process to a
lower-
• priority queue.
• The definition of a multilevel feedback queue scheduler makes
it the most general CPU-scheduling algorithm.
• It can be configured to match a specific system under design.
Unfortunately, it is also the most complex algorithm, since
defining the best scheduler requires some means by which to
select values for all the parameters.
Bilal Ahmed