Scheduling Algorithms in OS
Scheduling Algorithms in OS
Scheduling algorithms in operating systems are responsible for determining which process
should run next on the CPU. There are a variety of different scheduling algorithms, each with its
own advantages and disadvantages.
• Multiple-level queues: Multiple-level queues divide the ready queue into multiple queues,
each with its own scheduling algorithm. This allows different types of processes to be
scheduled in a way that optimizes performance.
• Multilevel feedback queues: Multilevel feedback queues are similar to multiple-level
queues, but they dynamically move processes between queues based on their behavior.
This can help to improve performance and fairness.
• Highest response ratio next (HRRN): HRRN schedules the process with the highest
response ratio next. The response ratio is calculated by dividing the waiting time by the
service time. This algorithm aims to minimize the average response time.
The choice of scheduling algorithm depends on the specific requirements of the operating
system and the types of applications that it will be running. For example, a real-time operating
system may use a priority scheduling algorithm to ensure that time-critical processes are
scheduled first. A batch operating system may use an FCFS algorithm to maximize throughput.
The following table compares the common scheduling algorithms in terms of their key
characteristics:
Algorithm Fairness Average waiting Average Overhead
time response
time
Scheduling algorithms are an essential part of operating systems. They play a critical role in
determining the performance and fairness of the system. The choice of scheduling algorithm
depends on the specific requirements of the operating system and the types of applications that
it will be running.
In each of these examples, the first person or thing to arrive is served first. This is a simple and
fair way to schedule things, but it can lead to long waiting times for those who arrive later,
especially if there are a lot of people or things waiting.
For example, if you arrive at a checkout counter with a full cart of groceries, you may have to
wait a long time if there are a lot of people in line ahead of you. Similarly, if you arrive at a
popular concert just as the doors open, you may have to wait a long time to get in.
Despite its limitations, FCFS is a widely used scheduling algorithm because it is simple to
implement and understand. It is also fair, as it ensures that everyone gets served in the order in
which they arrive.
However, FCFS scheduling can lead to delays for airplanes that arrive later in the queue. For
example, if a thunderstorm causes a delay in takeoffs, the airplanes that arrived first will be
delayed by the storm, even if they are ready to take off.
To mitigate the effects of delays, air traffic controllers often use a combination of FCFS and
other scheduling algorithms. For example, they may give priority to airplanes that are carrying
passengers with medical emergencies. They may also give priority to airplanes that are carrying
a lot of fuel, as they need to land sooner to avoid running out of fuel.
Advantages of FCFS
• Fairness: FCFS is a fair algorithm, as all processes are scheduled in the order in which
they arrive. This means that no process is given preferential treatment.
• Simplicity: FCFS is a simple algorithm to understand and implement. This makes it a
good choice for small systems or systems with limited resources.
• Low overhead: FCFS has low overhead, as there is no need to maintain complex data
structures or perform complex calculations to determine which process to schedule next.
Disadvantages of FCFS
• Long waiting times for long-running processes: FCFS can lead to long waiting times
for long-running processes, as they have to wait for all the shorter processes to finish
executing first. This can be a problem for systems that need to respond to requests
quickly, such as interactive systems.
• Poor performance for systems with a high mix of process types: FCFS can perform
poorly for systems with a high mix of process types, such as systems that run both CPU-
bound and I/O-bound processes. This is because CPU-bound processes can block I/O-
bound processes for long periods of time, which can lead to increased waiting times for all
processes.
Formula
The following formula can be used to calculate the average waiting time for the FCFS
scheduling algorithm:
Examples
Exercises
3. Compare the average waiting times for the two examples in questions 1 and 2. What can
you conclude about the FCFS scheduling algorithm?
Answers
Definition
The shortest remaining time (SRT) scheduling algorithm is a preemptive scheduling algorithm
that schedules the process with the shortest remaining burst time next. The SRT algorithm is
also known as the shortest job next (SJN) algorithm.
Formula
To calculate the SRT, the operating system maintains a queue of all the processes that
are ready to run. The queue is sorted in increasing order of remaining burst time. The
operating system then selects the process at the head of the queue and schedules it for
execution.
The SRT algorithm is a preemptive algorithm, which means that processes can be
interrupted and another process can be scheduled to run. This is necessary to ensure
that the process with the shortest remaining burst time is always scheduled.
Strengths
Weaknesses
The SRT algorithm also has some weaknesses, including:
Real-life applications
The SRT scheduling algorithm is often used in real-time operating systems, where it is important
to minimize the response time of processes. It is also used in some general-purpose operating
systems, such as Linux and Windows.
• Real-time operating systems: The SRT scheduling algorithm is often used in real-time
operating systems (RTOSs), where it is important to minimize the response time of
processes. RTOSs are used in a wide variety of systems, including industrial control
systems, medical devices, and aerospace systems.
• Air traffic control systems: Air traffic control systems use the SRT scheduling algorithm
to prioritize and schedule the landing of aircraft. This helps to ensure that aircraft land
safely and efficiently.
• Packet scheduling in networks: The SRT scheduling algorithm can also be used to
schedule packets in networks. This can help to improve the performance of networks and
reduce latency.
• Job scheduling systems: Some job scheduling systems use the SRT scheduling
algorithm to schedule jobs on compute clusters. This can help to improve the utilization of
compute resources and reduce the time it takes to complete jobs.
Here is an example of how the SRT scheduling algorithm can be used in a real-life scenario:
Imagine a hospital emergency room (ER) where patients are arriving with different medical
conditions and requiring different levels of care. The ER doctor can be thought of as the CPU
scheduler, and the patients can be thought of as the processes. The SRT scheduling algorithm
can be used to prioritize the patients and schedule them for treatment in the most efficient way
possible.
For example, if a patient arrives with a life-threatening injury, they would be given the highest
priority and scheduled for treatment immediately. Other patients with less severe injuries would
be scheduled for treatment based on their remaining treatment time. This would help to ensure
that all patients receive the care they need as quickly as possible.
Examples
6 | P1 | 10 | 4
6 | P3 | 2 | 0
7 | P4 | 6 | 5
8 | P2 | 5 | 4
9 | P4 | 6 | 4
10 | P2 | 5 | 3
11 | P4 | 6 | 3
12 | P2 | 5 | 2
13 | P4 | 6 | 2
14 | P2 | 5 | 1
15 | P4 | 6 | 1
16 | P2 | 5 | 0
In this example, the SRT algorithm schedules the process with the shortest remaining burst time
next. This means that P3 is scheduled first, followed by P2, P4, and P1.
Exercises
Exercise 1
Consider the following table of arrival time and burst time for four processes:
P1 0 10
P2 3 5
Process Arrival time Burst time
P3 4 2
P4 5 6
Draw a Gantt chart to show how the SRT scheduling algorithm would schedule these
processes.
Exercise 2
Calculate the average waiting time and average turnaround time for the processes in the
previous exercise.
Exercise 3
Solutions
Solution to Exercise 1
The following Gantt chart shows how the SRT scheduling algorithm would schedule the four
processes in the table:
Time | Process
-----|---------
0-2 | P3
2-7 | P2
7-13 | P4
13-15 | P1
Solution to Exercise 2
To calculate the average waiting time and average turnaround time for the processes, we can
use the following formulas:
Therefore, the average waiting time and average turnaround time for the processes are as
follows:
Solution to Exercise 3
• Starvation of long-running processes: Since the SRT algorithm always schedules the
process with the shortest remaining burst time next, long-running processes may be
starved of CPU time.
• Increased overhead due to context switching: The SRT algorithm requires frequent
context switching, which can increase overhead.
• Complexity: The SRT algorithm is more complex to implement than other scheduling
algorithms, such as first-come-first-served (FCFS).
• Unpredictability: The SRT algorithm is unpredictable, as it is difficult to predict which
process will be scheduled next. This can make it difficult to optimize the system for
performance and fairness.
• Favoritism of short processes: The SRT algorithm favors short processes over long
processes. This can lead to unfairness for long-running processes, which may be delayed
for long periods of time.
• Priority inversion: The SRT algorithm can lead to priority inversion, where a low-priority
process is scheduled ahead of a high-priority process. This can occur when the low-
priority process has a shorter remaining burst time than the high-priority process.