0% found this document useful (0 votes)
6 views

Scheduling Algorithms in OS

Schedule Algorithms of Operating system

Uploaded by

t62056841
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Scheduling Algorithms in OS

Schedule Algorithms of Operating system

Uploaded by

t62056841
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Scheduling Algorithms

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.

Some of the most common scheduling algorithms include:

• First-come, first-served (FCFS): FCFS is the simplest scheduling algorithm. It schedules


processes in the order in which they arrive. This algorithm is fair, but it can lead to long
waiting times for long-running processes.
• Shortest-job-next (SJN): SJN schedules the process with the shortest burst time next.
This algorithm minimizes the average waiting time, but it can also lead to starvation of
long-running processes.
• Priority scheduling: Priority scheduling assigns a priority to each process and schedules
the process with the highest priority next. This algorithm can be used to favor certain
processes, such as system processes or interactive processes. However, it can also lead
to starvation of lower-priority processes.
• Round robin (RR): RR schedules processes in a round-robin fashion, giving each
process a fixed amount of time to run. This algorithm is fair and ensures that all processes
get a chance to run. However, it can lead to overhead due to context switching.

Other scheduling algorithms include:

• 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.

Comparison of Scheduling Algorithms

The following table compares the common scheduling algorithms in terms of their key
characteristics:
Algorithm Fairness Average waiting Average Overhead
time response
time

FCFS Fair Long for long- Fair Low


running
processes

SJN Unfair to long- Short Short Medium


running processes

Priority Favorable to high- Depends on Depends on Medium


priority processes priorities priorities

Round Fair Medium Medium High


robin

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.

First-Come, First-served scheduling algorithm


Definition

The first-come, first-served (FCFS) scheduling algorithm is a non-preemptive scheduling


algorithm that schedules processes in the order in which they arrive. This means that the
process that arrives first is scheduled first, the process that arrives second is scheduled second,
and so on.

Real-life examples of FCFS

Real-life examples of the First-come, first-served (FCFS) scheduling algorithm include:

• Queues at a checkout counter in a supermarket


• Lines of people waiting to board a bus
• Tickets to a popular concert or event
• Patients waiting to see a doctor
• Airplanes waiting to take off or land
• Jobs waiting to be printed on a printer
• Processes waiting to be executed on a CPU

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.

• Queueing systems: FCFS is commonly used in queuing systems, such as supermarket


queues and bank queues. This is because it is a fair algorithm that ensures that everyone
is served in the order in which they arrive.
• Batch processing systems: FCFS is also commonly used in batch processing systems,
such as systems that print reports or process payroll data. This is because batch
processing systems typically run long-running jobs, and FCFS ensures that all jobs are
processed eventually.
• Embedded systems: FCFS is also sometimes used in embedded systems, such as the
systems that control traffic lights or microwave ovens. This is because embedded systems
typically have limited resources, and FCFS is a simple and efficient algorithm.

Here is a specific example of how FCFS is used in a real-world system:

Airplanes waiting to take off or land


When airplanes are waiting to take off or land, they are typically scheduled in FCFS order. This
means that the first airplane to arrive in the queue will be the first to take off or land. This is
done to ensure safety and order, as it prevents airplanes from colliding.

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:

Average waiting time = (Total waiting time of all processes) / (Number


of processes)

Examples

Consider the following example of the FCFS scheduling algorithm:


Process | Arrival time | Burst time | Waiting time
------- | ----------- | ---------- | -----------
P1 | 0 | 10 | 0
P2 | 10 | 5 | 10
P3 | 15 | 20 | 25

The average waiting time for this example is calculated as follows:

Average waiting time = (0 + 10 + 25) / 3 = 11.67

Exercises

1. Consider the following example of the FCFS scheduling algorithm:

Process | Arrival time | Burst time | Waiting time


------- | ----------- | ---------- | -----------
P1 | 0 | 5 | 0
P2 | 5 | 10 | 5
P3 | 15 | 15 | 20

Calculate the average waiting time for this example.

2. Consider the following example of the FCFS scheduling algorithm:

Process | Arrival time | Burst time | Waiting time


------- | ----------- | ---------- | -----------
P1 | 0 | 20 | 0
P2 | 10 | 5 | 10
P3 | 20 | 10 | 30

Calculate the average waiting time for this example.

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

1. The average waiting time for the first example is 11.67.


2. The average waiting time for the second example is 20.
3. The FCFS scheduling algorithm is fair, but it can lead to long waiting times for long-
running processes. This is because long-running processes have to wait for all of the
shorter-running processes to finish executing before they can start executing.

Shortest Remaining Time (SRT) Scheduling Algorithm

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

SRT = shortest remaining burst time

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.

Once a process is scheduled, it runs until it completes or until it is preempted by a


process with a shorter remaining burst time. If a process is preempted, it is placed at the
end of the queue and the process with the shortest remaining burst time is scheduled
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

The SRT algorithm has a number of strengths, including:

• It minimizes the average waiting time for all processes.


• It is fair, as all processes have an equal chance of being scheduled.
• It is relatively simple to implement.

Weaknesses
The SRT algorithm also has some weaknesses, including:

• It can lead to starvation of long-running processes.


• It can increase overhead due to context switching.

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

Consider the following example of the SRT scheduling algorithm:

Time | Process | Burst time | Remaining burst time


-----|---------|-----------|---------------------
0 | P1 | 10 | 10
3 | P2 | 5 | 5
4 | P3 | 2 | 2
5 | P4 | 6 | 6

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:

Process Arrival time Burst time

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

Discuss the advantages and disadvantages of the SRT scheduling algorithm.

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:

Average waiting time = (Total waiting time) / (Number of processes)


Average turnaround time = (Total turnaround time) / (Number of
processes)

The waiting time for each process is as follows:

| Process | Waiting time | |---|---|---| | P1 | 13 | | P2 | 2 | | P3 | 0 | | P4 | 5 |

The turnaround time for each process is as follows:

| Process | Turnaround time | |---|---|---| | P1 | 15 | | P2 | 7 | | P3 | 2 | | P4 | 13 |

Therefore, the average waiting time and average turnaround time for the processes are as
follows:

Average waiting time = (13 + 2 + 0 + 5) / 4 = 5 ms


Average turnaround time = (15 + 7 + 2 + 13) / 4 = 9.25 ms

Solution to Exercise 3

Advantages of the SRT scheduling algorithm:

• It minimizes the average waiting time for all processes.


• It is fair, as all processes have an equal chance of being scheduled.
• It is relatively simple to implement.

Disadvantages of the SRT scheduling algorithm:

• 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.

You might also like