0% found this document useful (0 votes)
55 views17 pages

CPU Scheduling Algorithms Explained

The document discusses CPU scheduling, which is essential for managing tasks in an operating system, highlighting its importance in maximizing CPU utilization and minimizing response times. It outlines various scheduling algorithms, including First Come, First Serve (FCFS), Shortest Job First (SJF), and Shortest Remaining Time First (SRTF), detailing their mechanics, advantages, and disadvantages. Additionally, it provides examples and calculations for average waiting and turnaround times for different scenarios.

Uploaded by

bgmiuserpro
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)
55 views17 pages

CPU Scheduling Algorithms Explained

The document discusses CPU scheduling, which is essential for managing tasks in an operating system, highlighting its importance in maximizing CPU utilization and minimizing response times. It outlines various scheduling algorithms, including First Come, First Serve (FCFS), Shortest Job First (SJF), and Shortest Remaining Time First (SRTF), detailing their mechanics, advantages, and disadvantages. Additionally, it provides examples and calculations for average waiting and turnaround times for different scenarios.

Uploaded by

bgmiuserpro
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

OPERATING SYSTEM -SEP

CPU SCHEDULING
CPU scheduling is a process used by the operating system to decide which task or process gets to use
the CPU at a particular 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. The following are different purposes of a CPU
scheduling time.
• Maximize the CPU utilization
• Minimize the response and waiting time of the process.
What is the Need for a CPU Scheduling Algorithm?
CPU scheduling is the process of deciding which process will own the CPU to use while another
process is suspended. The main function of CPU scheduling is to ensure that whenever the CPU
remains idle, the OS has at least selected one of the processes available in the ready-to-use line.
In Multiprogramming, if the long-term scheduler selects multiple I/O binding processes then most of
the time, the CPU remains idle. The function of an effective program is to improve resource
utilization.
Terminologies Used in CPU Scheduling
• Arrival Time: The time at which the process arrives in the ready queue.
• Completion Time: The time at which the 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.
Different CPU Scheduling algorithms have different structures and the choice of a particular
algorithm depends on a variety of factors.
• CPU Utilization: The main purpose of any CPU algorithm is to keep the CPU as busy as
possible. Theoretically, CPU usage can range from 0 to 100 but in a real-time system, it varies
from 40 to 90 percent depending on the system load.
• Throughput: The average CPU performance is the number of processes performed and
completed during each unit. This is called throughput. The output may vary depending on the
length or duration of the processes.
• Turn Round Time: For a particular process, the important conditions are how long it takes to
perform that process. The time elapsed from the time of process delivery to the time of
completion is known as the conversion time. Conversion time is the amount of time spent
waiting for memory access, waiting in line, using CPU, and waiting for I / O.
• Waiting Time: The Scheduling algorithm does not affect the time required to complete the
process once it has started performing. It only affects the waiting time of the process i.e. the
time spent in the waiting process in the ready queue.
• Response Time: In a collaborative system, turn around time is not the best option. The
process may produce something early and continue to computing the new results while the
previous results are released to the user. Therefore another method is the time taken in the
submission of the application process until the first response is issued. This measure is called
response time.
OPERATING SYSTEM -SEP

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.

First Come, First Serve (FCFS)


First Come, First Serve (FCFS) is one of the simplest types of CPU scheduling algorithms. It is
exactly what it sounds like: processes are attended to in the order in which they arrive in the ready
queue, much like customers lining up at a grocery store.
FCFS Scheduling is a non-preemptive algorithm, meaning once a process starts running, it cannot be
stopped until it voluntarily relinquishes the CPU, typically when it terminates or performs I/O. This
method treats all processes equally, without priority distinctions.
How Does FCFS Work?
The mechanics of FCFS are straightforward:
1. Arrival: Processes enter the system and are placed in a queue in the order they arrive.
2. Execution: The CPU takes the first process from the front of the queue, executes it until it is
complete, and then removes it from the queue.
3. Repeat: The CPU takes the next process in the queue and repeats the execution process.
This continues until there are no more processes left in the queue.
Example of FCFS CPU Scheduling:
To understand the First Come, First Served (FCFS) scheduling algorithm effectively, we’ll use two
examples –
• one where all processes arrive at the same time,
• another where processes arrive at different times.
We’ll create Gantt charts for both scenarios and calculate the turnaround time and waiting time for
each process.
OPERATING SYSTEM -SEP

Scenario 1: Processes with Same Arrival Time


Consider the following table of arrival time and burst time for three processes P1, P2 and P3

Process Arrival Time Burst Time

p1 0 5

p2 0 3

p3 0 8

Step-by-Step Execution:
1. P1 will start first and run for 5 units of time (from 0 to 5).
2. P2 will start next and run for 3 units of time (from 5 to 8).
3. P3 will run last, executing for 8 units (from 8 to 16).
Now, let’s calculate average waiting time and turn around time:
Turnaround Time = Completion Time - Arrival Time
Waiting Time = Turnaround Time - Burst Time
AT : Arrival Time
BT : Burst Time or CPU Time
TAT : Turn Around Time
WT : Waiting Time

Processes AT BT CT TAT WT

P1 0 5 5 5-0 = 5 5-5 = 0

P2 0 3 8 8-0 = 8 8-3 = 5

P3 0 8 16 16-0 = 16 16-8 = 8

• Average Turn around time = (5 + 8 + 16)/3 = 29/3 = 9.66 ms


• Average waiting time = (0 + 5 + 8)/3 = 13/3 = 4.33 ms
OPERATING SYSTEM -SEP

Scenario 2: Processes with Different Arrival Times

Consider the following table of arrival time and burst time for three processes P1, P2 and P3

Process Burst Time (BT) Arrival Time (AT)

P1 5 ms 2 ms

P2 3 ms 0 ms

P3 4 ms 4 ms

Step-by-Step Execution:
• P2 arrives at time 0 and runs for 3 units, so its completion time is:
Completion Time of P2=0+3=3
• P1 arrives at time 2 but has to wait for P2 to finish. P1 starts at time 3 and runs for 5 units. Its
completion time is:
Completion Time of P1=3+5=8
• P3 arrives at time 4 but has to wait for P1 to finish. P3 starts at time 8 and runs for 4 units. Its
completion time is:
Completion Time of P3=8+4=12

Now, lets calculate average waiting time and turn around time:

Completion Time Turnaround Time (TAT = CT Waiting Time (WT = TAT


Process (CT) – AT) – BT)

P2 3 ms 3 ms 0 ms

P1 8 ms 6 ms 1 ms

P3 12 ms 8 ms 4 ms
OPERATING SYSTEM -SEP

• Average Turnaround time = 5.67


• Average waiting time = 1.67
Advantages of FCFS
• The simplest and basic form of CPU Scheduling algorithm
• Every process gets a chance to execute in the order of its arrival. This ensures that no process
is arbitrarily prioritized over another.
• Easy to implement, it doesn’t require complex data structures.
• Since processes are executed in the order they arrive, there’s no risk of starvation
• It is well suited for batch systems where the longer time periods for each process are often
acceptable.
Disadvantages of FCFS
• As it is a Non-preemptive CPU Scheduling Algorithm, FCFS can result in long waiting times,
especially if a long process arrives before a shorter one. This is known as the convoy effect,
where shorter processes are forced to wait behind longer processes, leading to inefficient
execution.
• The average waiting time in the FCFS is much higher than in the others
• Since FCFS processes tasks in the order they arrive, short jobs may have to wait a long time if
they arrive after longer tasks, which leads to poor performance in systems with a mix of long
and short tasks.
• Processes that are at the end of the queue, have to wait longer to finish.
• It is not suitable for time-sharing operating systems where each process should get the same
amount of CPU time.
Shortest Job First (SJF) or Shortest Job Next (SJN)
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.
Implementation of SJF Scheduling
• Sort all the processes according to the arrival time.
• Then select that process that has minimum arrival time and minimum Burst time.
• After completion of the process make a pool of processes (a ready queue) that arrives
afterward till the completion of the previous process and select that process in that queue
which is having minimum Burst time.
Characteristics of SJF Scheduling
• Shortest Job first has the advantage of having a minimum average waiting time among all
operating system scheduling algorithms.
• It is associated with each task as a unit of time to complete.
OPERATING SYSTEM -SEP

• It may cause starvation if shorter processes keep coming. This problem can be solved using
the concept of ageing.
Example of Non Pre-emptive Shortest Job First CPU Scheduling Algorithm
Example: Consider the following table of arrival time and burst time for three processes P1, P2
and P3.

Process Burst Time Arrival Time

P1 6 ms 0 ms

P2 8 ms 2 ms

P3 3 ms 4 ms

Step-by-Step Execution:
1. Time 0-6 (P1): P1 runs for 6 ms (total time left: 0 ms)
2. Time 6-9 (P3): P3 runs for 3 ms (total time left: 0 ms)
3. Time 9-17 (P2): P2 runs for 8 ms (total time left: 0 ms)
As we know,
• Turn Around time = Completion time - arrival time
• Waiting Time = Turn around time - burst time
Now, lets calculate average waiting time and turn around time:

Arrival Burst Turn Waiting


Time Time Completion Around Time
Process (AT) (BT) Time (CT) Time (TAT) (WT)

P1 0 6 6 6-0 = 6 6-6 = 0

P2 2 8 17 17-2 = 15 15-8 = 7

P3 4 3 9 9-4 = 5 5-3 = 2
OPERATING SYSTEM -SEP

• Average Turn around time = (6 + 15 + 5)/3 = 8.6 ms


• Average waiting time = ( 2 + 0 + 7 )/3 = 9/3 = 3 ms
Advantages of SJF Scheduling
• SJF is better than the First come first serve(FCFS) algorithm as it reduces the average waiting
time.
• SJF is generally used for long term scheduling.
• It is suitable for the jobs running in batches, where run times are already known.
• SJF is probably optimal in terms of average Turn Around Time (TAT).
Disadvantages of SJF Scheduling
• SJF may cause very long turn-around times or starvation.
• In SJF job completion time must be known earlier.
• Many times it becomes complicated to predict the length of the upcoming CPU request.
Shortest Remaining Time First (Preemptive SJF)
In SRTF, the process with the least time left to finish is selected to run. The running process will
continue until it finishes or a new process with a shorter remaining time arrives. This way, the
process that can finish the fastest is always given priority.
Advantages of SRTF Scheduling
1. Minimizes Average Waiting Time: SRTF reduces the average waiting time by prioritizing
processes with the shortest remaining execution time.
2. Efficient for Short Processes: Shorter processes get completed faster, improving overall
system responsiveness.
3. Ideal for Time-Critical Systems: It ensures that time-sensitive processes are executed quickly.
Disadvantages of SRTF Scheduling
1. Starvation of Long Processes: Longer processes may be delayed indefinitely if shorter
processes keep arriving.
2. Difficult to Predict Burst Times: Accurate prediction of process burst times is challenging and
affects scheduling decisions.
3. High Overhead: Frequent context switching can increase overhead and slow down system
performance.
4. Not Suitable for Real-Time Systems: Real-time tasks may suffer delays due to frequent
preemptions.
Examples to show working of Pre-emptive Shortest Job First CPU Scheduling Algorithm
Scenario 1: Processes with Same Arrival Time
Example: Consider the following table of arrival time and burst time for three processes P1, P2 and
P3.
OPERATING SYSTEM -SEP

Process Burst Time Arrival Time

P1 6 ms 0 ms

P2 8 ms 0 ms

P3 5 ms 0 ms

Step-by-Step Execution:
1. Time 0-5 (P3): P3 runs for 5 ms (total time left: 0 ms) as it has shortest remaining time left.
2. Time 5-11 (P1): P1 runs for 6 ms (total time left: 0 ms) as it has shortest remaining time left.
3. Time 11-19 (P2): P2 runs for 8 ms (total time left: 0 ms) as it has shortest remaining time left.
Gantt chart :

Arrival Burst
Time Time
Completion Turn Around Waiting Time
Process (AT) (BT) Time (CT) Time (TAT) (WT)

P1 0 6 11 11-0 = 11 11-6 = 5

P2 0 8 19 19-0 = 19 19-8 = 11

P3 0 5 5 5-0 = 5 5-5 = 0

Now,
• Average Turn around time = (11 + 19 + 5)/3 = 11.6 ms
• Average waiting time = (5 + 0 + 11 )/3 = 16/3 = 5.33 ms

Scenario 2: Processes with Different Arrival Times


Consider the following table of arrival time and burst time for three processes P1, P2 and P3.
OPERATING SYSTEM -SEP

Process Burst Time Arrival Time

P1 6 ms 0 ms

P2 3 ms 1 ms

P3 7 ms 2 ms

Step-by-Step Execution:
1. Time 0-1 (P1): P1 runs for 1 ms (total time left: 5 ms) as it has shortest remaining time left.
2. Time 1-4 (P2): P2 runs for 3 ms (total time left: 0 ms) as it has shortest remaining time left
among P1 and P2.
3. Time 4-9 (P1): P1 runs for 5 ms (total time left: 0 ms) as it has shortest remaining time left
among P1 and P3.
4. Time 9-16 (P3): P3 runs for 7 ms (total time left: 0 ms) as it has shortest remaining time left.
Gantt chart :

Arrival Burst
Time Time Completion Turn Around Waiting Time
Process (AT) (BT) Time (CT) Time (TAT) (WT)

P1 0 6 9 9-0 = 9 9-6 = 3

P2 1 3 4 4-1 = 3 3-3 = 0

P3 2 7 16 16-2 = 14 14-7 = 7

• Average Turn around time = (9 + 14 + 3)/3 = 8.6 ms


• Average waiting time = (3 + 0 + 7 )/3 = 10/3 = 3.33 ms
OPERATING SYSTEM -SEP

PRIORITY SCHEDULING
It is one of the most common scheduling algorithms used by the operating system to schedule
processes based on their priority. Each process is assigned a priority. The process with the highest
priority is to be executed first and so on.
Processes with the same priority are executed on a first-come first served basis. Priority can be
decided based on memory requirements, time requirements or any other resource requirement.
Also priority can be decided on the ratio of average I/O to average CPU burst time.
Priority Scheduling can be implemented in two ways:
• Non-Preemptive Priority Scheduling
• Preemptive Priority Scheduling
Non-Preemptive Priority Scheduling
In Non-Preemptive Priority Scheduling, the CPU is not taken away from the running process.
Even if a higher-priority process arrives, the currently running process will complete first.
Ex: A high-priority process must wait until the currently running process finishes.
Example of Non-Preemptive Priority Scheduling:
Consider the following table of arrival time and burst time for three processes P1, P2 and P3:
Note: Lower number represents higher priority.

Process Arrival Time Burst Time Priority

P1 0 4 2

P2 1 2 1

P3 2 6 3

Step-by-Step Execution:
• At Time 0: Only P1 has arrived. P1 starts execution as it is the only available process, and it
will continue executing till t = 4 because it is a non-preemptive approach.
• At Time 4: P1 finishes execution. Both P2 and P3 have arrived. Since P2 has the highest
priority (Priority 1), it is selected next.
• At Time 6: P2 finishes execution. The only remaining process is P3, so it starts execution.
• At Time 12: P3 finishes execution.
Now, lets calculate average waiting time and turn around time:
OPERATING SYSTEM -SEP

Waiting
Turnaround Time
Arrival Burst Completion Time (CT - (TAT -
Process Time Time Time AT) BT)

P1 0 4 4 4 0

P2 1 2 6 5 3

P3 2 6 12 10 4

• Average Turnaround Time = 6.33


• Average Waiting Time = 2.33
Preemptive Priority Scheduling
In Preemptive Priority Scheduling, the CPU can be taken away from the currently running
process if a new process with a higher priority arrives.
Ex: A low-priority process is running, and a high-priority process arrives; the CPU immediately
switches to the high-priority process.
Example of Preemptive Priority Scheduling (Same Arrival Time)
Consider the following table of arrival time and burst time for three processes P1, P2 and P3:
Note: Higher number represents higher priority.

Process Arrival Time Burst Time Priority

P1 0 7 2

P2 0 4 1

P3 0 6 3

Step-by-Step Execution:
OPERATING SYSTEM -SEP

• At Time 0: All processes arrive at the same time. P3 has the highest priority (Priority 3), so it
starts execution.
• At Time 6: P3 completes execution. Among the remaining processes, P1 (Priority 2) has a
higher priority than P2, so P1 starts execution.
• At Time 13: P1 completes execution. The only remaining process is P2 (Priority 1), so it
starts execution.
• At Time 17: P2 completes execution. All processes are now finished.
Now, lets calculate average waiting time and turn around time:

Waiting
Turnaround Time
Arrival Burst Completion Time (CT - (TAT -
Process Time Time Time AT) BT)

P1 0 7 13 13 6

P2 0 4 17 17 13

P3 0 6 6 6 0

• Average Turnaround Time = 12


• Average Waiting Time = 6.33
Example of Preemptive Priority Scheduling (Different Arrival Time)
Consider the following table of arrival time and burst time for three processes P1, P2 and P3:

Process Arrival Time Burst Time Priority

P1 0 6 2

P2 1 4 3

P3 2 5 1
OPERATING SYSTEM -SEP

Step-by-Step Execution:
• At Time 0: Only P1 has arrived, so it starts execution.
• At Time 1: P2 arrives with a higher priority (Priority 3) than P1. P1 is preempted, and P2
starts execution.
• At Time 5: P2 completes execution. Both P1 and P3 are available. P1 has the higher priority
(Priority 2), so it starts execution.
• At Time 10: P1 completes execution. P3 resumes execution to finish its remaining burst time.
• At Time 15: P3 completes execution. All processes are now finished.

Now, lets calculate average waiting time and turn around time:

Waiting
Turnaround Time
Arrival Burst Completion Time (CT - (TAT -
Process Time Time Time AT) BT)

P1 0 6 10 10 4

P2 1 4 5 4 0

P3 2 5 15 13 8

• Average Turnaround Time = 9


• Average Waiting Time = 4
ROUND ROBIN SCHEDULING
Round Robin Scheduling is a method used by operating systems to manage the execution time
of multiple processes that are competing for CPU attention. It is called "round robin" because the
system rotates through all the processes, allocating each of them a fixed time slice or "quantum",
regardless of their priority.
The primary goal of this scheduling method is to ensure that all processes are given an equal
opportunity to execute, promoting fairness among tasks.
Here's a simple breakdown:
OPERATING SYSTEM -SEP

• Process Arrival: Processes enter the system and are placed in a queue.
• Time Allocation: Each process is given a certain amount of CPU time, called a quantum.
• Execution: The process uses the CPU for the allocated time.
• Rotation: If the process completes within the time, it leaves the system. If not, it goes back to
the end of the queue.
• Repeat: The CPU continues to cycle through the queue until all processes are completed.
Advantages of Round Robin Scheduling
• Fairness: Each process gets an equal share of the CPU.
• Simplicity: The algorithm is straightforward and easy to implement.
• Responsiveness: Round Robin can handle multiple processes without significant delays,
making it ideal for time-sharing systems.
Disadvantages of Round Robin Scheduling:
• Overhead: Switching between processes can lead to high overhead, especially if the quantum
is too small.
• Underutilization: If the quantum is too large, it can cause the CPU to feel unresponsive as it
waits for a process to finish its time.
Example of Round Robin Scheduling Algorithm:
To understand the Round Robin Scheduling algorithm, let’s consider the following two scenarios:
Scenario 1: Processes with Same Arrival Time
Consider the following table of arrival time and burst time for three processes P1, P2 and P3 and
given Time Quantum = 2 ms

Process Burst Time Arrival Time

P1 4 ms 0 ms

P2 5 ms 0 ms

P3 3 ms 0 ms

Step-by-Step Execution:
1. Time 0-2 (P1): P1 runs for 2 ms (total time left: 2 ms).
2. Time 2-4 (P2): P2 runs for 2 ms (total time left: 3 ms).
3. Time 4-6 (P3): P3 runs for 2 ms (total time left: 1 ms).
4. Time 6-8 (P1): P1 finishes its last 2 ms.
OPERATING SYSTEM -SEP

5. Time 8-10 (P2): P2 runs for another 2 ms (total time left: 1 ms).
6. Time 10-11 (P3): P3 finishes its last 1 ms.
7. Time 11-12 (P2): P2 finishes its last 1 ms.
Gantt Chart:

Now, lets calculate average waiting time and turn around time:
• Turnaround Time = Completion Time - Arrival Time
• Waiting Time = Turnaround Time - Burst Time

Processes AT BT CT TAT WT

P1 0 4 8 8-0 = 8 8-4 = 4

P2 0 5 12 12-0 = 12 12-5 = 7

P3 0 3 11 11-0 = 11 11-3 = 8

• Average Turn around time = (8 + 12 + 11)/3 = 31/3 = 10.33 ms


• Average waiting time = (4 + 7 + 8)/3 = 19/3 = 6.33 ms
Scenario 2: Processes with Different Arrival Times
Consider the following table of arrival time and burst time for three processes P1, P2 and P3 and
given Time Quantum = 2

Process Burst Time (BT) Arrival Time (AT)

P1 5 ms 0 ms

P2 2 ms 4 ms
OPERATING SYSTEM -SEP

Process Burst Time (BT) Arrival Time (AT)

P3 4 ms 5 ms

Step-by-Step Execution:
• Time 0-2 (P1 Executes):
o P1 starts execution as it arrives at 0 ms.
o Runs for 2 ms; remaining burst time = 5 - 2 = 3 ms.
o Ready Queue: [P1].
• Time 2-4 (P1 Executes Again):
o P1 continues execution since no other process has arrived yet.
o Runs for 2 ms; remaining burst time = 3 - 2 = 1 ms.
o P2 and P3 arrive at 4 ms.
o Ready Queue: [P2, P1].
• Time 4-6 (P2 Executes):
o P2 starts execution as it arrives at 4 ms.
o Runs for 2 ms; remaining burst time = 2 - 2 = 0 ms.
o Ready Queue: [P1, P3].
• Time 6-7 (P1 Executes):
o P1 starts execution.
o Runs for 1 ms; remaining burst time = 1 - 1 = 0 ms.
o Ready Queue: [P3].
• Time 7-9 (P3 Executes):
o P3 starts execution.
o Remaining burst time = 4 - 2 = 2 ms.
o Ready Queue: [P3].
• Time 9-11 (P3 Executes Again):
o P3 resumes execution and runs for 2 ms and complete its execution
o Remaining burst time = 2 - 2 = 0 ms.
o Ready Queue: [].
Now, lets calculate average waiting time and turn around time:
OPERATING SYSTEM -SEP

Completion Time Turnaround Time (TAT Waiting Time (WT =


Process (CT) = CT - AT) TAT - BT)

P1 7 ms 7 ms 2 ms

P2 6 ms 2 ms 0 ms

P3 11 ms 6 ms 2 ms

• Average Turn around time =7+2+6/3=15/3=5ms


• Average waiting time = 2+0+2/3=1.33ms

You might also like