0% found this document useful (0 votes)
3 views35 pages

Lecture 5 - Scheduling

Uploaded by

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

Lecture 5 - Scheduling

Uploaded by

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

PROCESS

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

Once a process starts, it runs to A process can be interrupted and


CPU Allocation
completion or wait for some event. moved to the ready queue.

Can be longer, especially for Generally shorter, as higher-priority


Response Time
short tasks. tasks can pre-empt others.

More complex, requiring careful


Complexity Simpler to implement.
handling of shared resources.

Resource May lead to inefficient CPU Typically more efficient, as it can


Utilization utilization. quickly switch tasks.

Suitable Batch systems and applications Interactive and real-time systems


Application that require predictable timing. requiring responsive behavior.
s
Bilal Ahmed
Scheduling Criteria
Different CPU-scheduling algorithms have different properties, and the
choice of a particular algorithm may favour one class of processes over
another. So, in order to efficiently select the scheduling algorithms
following criteria should be taken into consideration:
CPU Utilization
• Keeping the CPU as busy as possible.
Throughput
If the CPU is busy executing processes, then work is being done. One measure
of work is the number of processes that are completed per time unit, called
throughput.
Waiting time
Waiting time is the sum of the periods spent waiting in the ready queue.
Response Time
Is the time it takes to start responding, not the time it takes to
output the response.
It is desirable to
maximize CPU utilization and throughput
and to
minimize turnaround time, waiting time, and response time.

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.

• Completion Time (CT): Time at which process finishes its execution.

• Turn Around Time (TAT): Completion Time (CT) – Arrival Time (AT), Waiting Time + Burst Time
(BT)

• Waiting Time: Turn Around Time (TAT) – Burst Time (BT)


FCFS (FIRST COME FIRST SERVE)
• FCFS is the simplest scheduling algorithm, as the name suggest, the process that requests the
CPU first is allocated the CPU first.
• Implementation is managed by FIFO Queue.
• It is always non pre-emptive in nature.

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.

• Can be used for Background processes where execution is not urgent.


P. No AT BT TAT=CT-AT WT=TAT -BT

P0 0 100

P1 1 2
Average

P. No AT BT TAT=CT-AT WT=TAT -BT

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.

• Higher average waiting time and TAT compared to other algorithms.


Shortest Job First (SJF)(non-pre-emptive)
Shortest Remaining Time First (SRTF)/ (Shortest Next CPU Burst) (Pre-emptive)

• Whenever we make a decision of selecting the next process for CPU


execution, out of all available process, CPU is assigned to the process having
smallest burst time requirement.
• When the CPU is available, it is assigned to the process that has the smallest
next CPU burst. If there is a tie, FCFS is used to break tie.

Bilal Ahmed
Shortest Job First (SJF)(non-pre-emptive)

• It supports both version non-pre-emptive and pre-emptive (purely


greedy approach)
• In Shortest Job First (SJF)(non-pre-emptive) once a decision is made and
among the available process, the process with the smallest CPU burst is
scheduled on the CPU, it cannot be pre-empted even if a new process with
the smaller CPU burst requirement, then the remaining CPU burst of the
running process enter in the system.

Bilal Ahmed
Shortest Remaining Time First (SRTF)/ (Shortest Next CPU Burst) (Pre-emptive)

In Shortest Remaining Time First (SRTF) (Pre-emptive) whenever a


process enters in ready state, again we make a scheduling decision
weather, this new process with the smaller CPU burst requirement then
the remaining CPU burst of the running process and if it is the case then
the running process is pre-empted and new process is scheduled on the
CPU.

This version (SRTF) is also called optimal is


it guarantee minimal average waiting
time.
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time
(AT) (BT) (CT) (TAT) = CT - AT (WT) = TAT - BT
P0 1 7

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.

Note: - Specially use to support system process or important user process


Ageing: - a technique of gradually increasing the priority of processes that wait in
the system for long time. E.g. priority will increase after every 10 mins
Bilal Ahmed
Round Robin
• This algo is designed for time sharing systems, where it is not,
the idea to complete one process and then start another, but
to be responsive and divide time of CPU among the process in
the ready state(circular).
• The CPU scheduler goes around the ready queue, allocating
the CPU to each process for a maximum of 1 Time quantum say
q. Up to which a process can hold the CPU in one go, with in
which either a process terminates if process have a CPU burst
of less than given time quantum or context switch will be
executed and process must release the CPU voluntarily and
enter the ready queue and wait for the next chance.
• 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. Each process must wait no
longer than (n - 1) x q time units until its next time quantum.
P. No Arrival Time Burst Time Completion Time Turn Around Time Waiting Time (WT)
(AT) (BT) (CT) (TAT) = CT - AT = TAT - BT
P0 0 4
P1 1 5
P2 2 2
P3 3 1
P4 4 6
P5 6 3
Average

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.

• Here processes are easily classified into different groups.


• System process
• foreground (interactive) processes
• background (batch) 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

You might also like