Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views7 pages

CPU Scheduling Algorithm

Uploaded by

lekeglean
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)
4 views7 pages

CPU Scheduling Algorithm

Uploaded by

lekeglean
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/ 7

CPU Scheduling Algorithm

CPU Scheduling is a process that allows one process to use the CPU while another process is
delayed (in standby) due to unavailability of any resources such as I / O etc, thus making full use
of the CPU. The purpose of CPU Scheduling is to make the system more efficient, faster, and
fairer.

CPU scheduling is a key part of how an operating system works especially multi-programmed
OS. It decides which task (or process) the CPU should work on at any given 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. Whenever the CPU becomes idle, the operating system must select
one of the processes in the line ready for launch. The selection process is done by a CPU
scheduler. The Scheduler selects between memory processes ready to launch and assigns the
CPU to one of them

Terminologies Used in CPU Scheduling


Arrival Time: Time at which the process arrives in the ready queue.
Completion Time: Time at which process completes its execution.
Burst Time: Time required by a process for CPU execution.
Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time (W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time

“Gantt chart is a chart that illustrates a particular schedule, including the start and finish
times of each of the participating processes”

Different Types of CPU Scheduling Algorithms


There are mainly two types of scheduling methods:
Preemptive Scheduling: Preemptive scheduling is used when a process switches from running
state to ready state or from the waiting state to the ready state.
Non-Preemptive Scheduling: Non-Preemptive scheduling is used when a process terminates ,
or when a process switches from running state to waiting state.
FCFS
It is considered to be the simplest of all operating system scheduling algorithms. First come first
serve scheduling algorithm states that the process that requests the CPU first is allocated the
CPU first and is implemented by using FIFO queue.
Characteristics of FCFS
 FCFS supports non-preemptive CPU scheduling algorithms.
 Tasks are always executed on a First-come, First-serve concept.
 FCFS is easy to implement and use.
 This algorithm is not much efficient in performance, and the wait time is quite high.
Advantages of FCFS
 Easy to implement
 First come, first serve method
Disadvantages of FCFS
 FCFS suffers from Convoy effect.
 The average waiting time is much higher than the other algorithms.
Shortest job first scheduling
It is a scheduling process that selects the waiting process with the smallest execution time to
execute next. This scheduling method may or may not be preemptive. Significantly reduces the
average waiting time for other processes waiting to be executed. Note that a more appropriate
term for this scheduling method would be the shortest-next CPU-burst algorithm, because
scheduling depends on the length of the next CPU burst of a process, rather than its total length.

Characteristics

 This is also known as shortest job first, or SJF


 This is a non-preemptive, pre-emptive scheduling algorithm.
 Best approach to minimize waiting time.
 Easy to implement in Batch systems where required CPU time is known in advance.
 Impossible to implement in interactive systems where required CPU time is not known.
 The processer should know in advance how much time process will take.

Advantages of SJF
 As SJF reduces the average waiting time thus, it is better than the first come first serve
scheduling algorithm.
 SJF is generally used for long term scheduling
Disadvantages of SJF
 One of the demerit SJF has is starvation.
 Many times it becomes complicated to predict the length of the upcoming CPU request
Priority scheduling
A priority is associated with each process, and the CPU is allocated to the process with the
highest priority. Equal-priority processes are scheduled in FCFS order. An SJF algorithm is
simply a priority algorithm where the priority(p) is the inverse of the (predicted) next CPU burst.
The larger the CPU burst, the lower the priority, and vice versa. Note that we discuss scheduling
in terms of high priority and low priority. Priorities are generally indicated by some fixed range
of numbers, such as 0 to7 or 0 to 4,095. However, there is no general agreement on whether 0 is
the highest or lowest priority. Some systems use low numbers to represent low priority; others
use low numbers for high priority. This difference can lead to confusion. In this text, we assume
that low numbers represent high priority.
Advantages of priority scheduling

 Easy to use scheduling method


 Processes are executed on the basis of priority so high priority does not need to wait for
long which saves time
 This method provides a good mechanism where the relative important of each process
may be precisely defined.
 Suitable for applications with fluctuating time and resource requirements.
Disadvantages of priority scheduling

 Priority inversion: Priority inversion occurs when a low-priority process holds a resource
that a high-priority process requires. This can cause delays in the execution of high-
priority processes.
 Starvation: If the system is heavily loaded with high-priority processes, low-priority
processes may never get a chance to execute.
 Priority inversion avoidance techniques, such as priority inheritance or priority ceiling
protocols, can introduce additional complexity to the system.
 Setting priorities can be a difficult task, especially when there are many processes with
different priorities

Exercise
1) Consider the following processes, assumed to have arrived at time 0 in the order p1,
p2,p3,…, p5. Using priority scheduling calculate the average waiting time

Process Burst time Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Ans= 8.2ms
Exercise
Consider the following processes from P1 to P5 each has unique priority, burst time and arrival
time, calculate the average waiting time
Process Priority Burst time Arrival time
P1 1 4 0
P2 2 3 0
P3 1 7 6
P4 3 4 11
P5 2 2 12
Anx= 2.4ms
Exercise 2
Consider the set of processes with arrival time, CPU burst time and priority (0 is the highest
priority) shown below. Calculate average waiting time

Process ID Arrival time Burst time Priority


P1 0 11 2
P2 5 28 0
P3 12 2 3
P4 2 10 1
P5 9 16 4

Ans = 29ms

Shortest Remaining Time


hortest remaining time (SRT) is the preemptive version of the SJN algorithm. The processor is
allocated to the job closest to completion but it can be preempted by a newer ready job with
shorter time to completion. Impossible to implement in interactive systems where required CPU
time is not known.
It is often used in batch environments where short jobs need to give preference.
Round Robin priority scheduling
Round Robin is the preemptive process scheduling algorithm. Each process is provided a fix
time to execute, it is called a quantum. Once a process is executed for a given time period, it is
preempted and other process executes for a given time period. Context switching is used to save
states of preempted processes.
Advantages of Round Robin Scheduling
 There is fairness since every process gets an equal share of the CPU.
 The newly created process is added to the end of the ready queue.
 A round-robin scheduler generally employs time-sharing, giving each job a time slot or
quantum.
 Each process get a chance to reschedule after a particular quantum time in this
scheduling.

Disadvantages of Round Robin scheduling

 There is Larger waiting time and Response time.


 There is Low throughput.
 There is Context Switches.
 Gantt chart seems to come too big (if quantum time is less for scheduling. For Example:1
ms for big scheduling.)
 Time consuming scheduling for small quantum
Problems
1) Consider the following set of processes that arrive at time 0, with the length of the CPU
burst given in milliseconds. Calculate average waiting time using round robin(

Process Burst time


P1 24
P2 3
P3 3

2) Consider the following table of arrival time and burst time for four processes P1, P2, P3,
and P4 and given Time Quantum = 2. Calculate average waiting time using round robin
Process Burst time Arrival time
P1 5 0
P2 4 1
P3 2 2
P4 1 4
3) Consider the following table of arrival time and burst time for four processes P1, P2, P3,
P4 and P5and given Time Quantum = 2. Calculate average TAT and waiting time using
round robin
Process Arrival time Burst time
P1 0 5
P2 1 3
P3 2 1
P4 3 3
P5 4 3

You might also like