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

Lecture 3 - Process Scheduling

This document discusses process scheduling in operating systems. It covers different levels of scheduling like long-term, medium-term, and short-term scheduling. It also discusses non-preemptive and preemptive scheduling. Common scheduling algorithms like first come first served and priority scheduling are explained with examples. The goals and tradeoffs of different scheduling techniques are outlined.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Lecture 3 - Process Scheduling

This document discusses process scheduling in operating systems. It covers different levels of scheduling like long-term, medium-term, and short-term scheduling. It also discusses non-preemptive and preemptive scheduling. Common scheduling algorithms like first come first served and priority scheduling are explained with examples. The goals and tradeoffs of different scheduling techniques are outlined.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

CS 334: Principles of Operating

System

Lecture 3: Process Scheduling


Outline
• Introduction
• Long-term scheduling
• Medium-term scheduling
• Short-term scheduling
• Non-pre-emptive scheduling
• Pre-emptive scheduling
• Scheduling algorithms
• Multi-level scheduling algorithms
Introduction
• Process scheduling is an important component of
process management.
• It plays a big role in implementation of multi-
programming, multi-user and multi-tasking
environments.
• A multi-user environment expects a minimum response
time, so that every user gets their applications running.
• In multi-tasking systems, a user expects to open many
applications and every application should respond
within a minimum period of time.
• In real-time systems, processes are executed according
to their defined priorities. A higher-priority process
must always pre-empt the lower one
Scheduling Decision
The following events can trigger process scheduling
i. When an executing process finishes its execution and
exits
ii. When the executing process needs to wait for an I/O or
resource
iii. When an I/O or resource being used by any process is
released
iv. When a process finishes its allotted time slice
v. When an executing process creates a child process
vi. When a newly added process in the ready queue has
higher priority compared to that of the running
process
vii. If there is an error or exception in the process or
hardware
viii. When there is need to suspend some blocked process
and make space for a new process
Scheduling Levels
Long term scheduling
• When there is no available memory space, a job
entering a system for the first time will have to wait in
the disk
• The OS provides a job queue for keeping the waiting
jobs until a space in memory is available
• When the space is available, the job is moved from the
job queue to the ready queue
• The long-term schedular is also used when newly
created jobs need to be moved to the ready-suspended
queue
Scheduling Levels
Medium term scheduling
• Swapping processes from blocked queue to blocked-
suspend queue
• Moving processes from blocked-suspend queue to
ready-suspend queue when the I/O being waited
becomes available
• Scheduling process from the ready-suspend queue back
to the ready queue
Scheduling Levels
Short term scheduling
• Selecting the next process
to be executed by the
processor
• After selecting the process,
the schedular passes its
information to the
dispatcher function.
• The dispatcher finds the
location of the process PCB
and loads registers to
initiate the execution of the
process.
• Short-term scheduling is
very frequent as a process
goes through many states
before execution completes
Scheduling Types
There are two main types of scheduling
Non pre-emptive
• A process assigned to the processor is allowed to
execute to completion.
• The system cannot take away the processor from the
process until it exits.
• The process can voluntarily release the processor, for
example, when waiting for I/O
Scheduling Types
Pre-emptive
• A running process can be interrupted in between.
• The interrupted process is put back into the ready queue
and will be re-scheduled according to the scheduling
policy

Pre-emption takes place in the following situations:


i. When a new process with higher priority arrives in the
ready queue.
ii. When a resource or I/O being waited upon is released
iii. A timer interrupt occurs.
Scheduling Types
Pre-emptive
• Pre-emptive scheduling is used to implement multi-user
time-sharing systems, where more than one user
process needs computing resources.
• A timer clock is implemented to send interrupts to
running processes after a fixed period of time.
• Also used to implement real-time systems, where high
priority processes need to be assigned a processor
immediately.
• Higher priorities are assigned to critical modules so that
the critical processes can pre-empt the less critical
running process
Scheduling Types
Advantage of pre-emptive scheduling
• Better process management in a multiprogramming and
multiuser environment

Disadvantages of pre-emptive scheduling


• High cost/complexity in implementation due to addition
of timer clock
• Reduced CPU performance due to time wasted in the
frequent context switching
Scheduling Algorithms
Design Requirements
Scheduling algorithms are designed to satisfy one of the
following requirements
1. Turnaround Time
• The time elapsed between the submission of a job and
its termination is called the turnaround time.
• It includes the time spent in waiting in the ready queue,
blocked queue and the execution time.
• Scheduling algorithms are designed for a minimum
turnaround time.
Scheduling Algorithms
Design Requirements

2. Waiting Time
• The total time spent in the ready queue by a process.
• Scheduling algorithms are designed to have a minimum
waiting time.

3. Response Time
• The time period between the submission of a process
and the first response given by the process to the user.
• Scheduling algorithms are designed to have a response
time that is within the acceptable range.
Scheduling Algorithms
Design Requirements

4. Throughput
• The number of processes completed in a unit time.
• Scheduling algorithm are designed to maximize the
throughput of the system.

5. CPU Utilization
• The percentage of time that the CPU is busy in executing
the processes.
• The fundamental requirement is that the processor
should be busy most of the time.
Scheduling Algorithms
Design Requirements

6. Fairness
• All processes in the system should get a chance to the
CPU, lower priority processes should not be ignored
indefinitely.
• A scheduling algorithm should not allow any process to
starve.

7. Balance
• At a particular instant of time, all resources should be
used in balance.
• A scheduling algorithm should select a proper mix of
CPU-bound and I/O-bound processes
Scheduling Algorithms
First Come First Served (FCFS)
• The process that comes first to the ready queue will be
served first.
• The arriving process is added onto the tail of the queue
and the process at the head of the queue is dispatched
to the processor for execution.
• In this way, a ready queue is implemented as a linked list
wherein the order of arrival of the processes is
maintained with the simple logic of first in, first out
(FIFO).
• This scheduling policy is non pre-emptive, a process
which has arrived first will be executed first to its
completion
• A running process is moved back to the tail of the ready
queue if interrupted.
Scheduling Algorithms
First Come First Served (FCFS)
Example 3.1
Scheduling Algorithms
First Come First Served (FCFS)
Example 3.1 solution

Process Arrival Execution Execution Waiting Turn Normalized


Time Time (x) Start Time Time around Turn
Time (t) around
Time (x/t)
P1 0 5 0 0 5 1
P2 2 4 5 3 7 1.75
P3 3 7 9 6 13 1.85
P4 5 6 16 11 17 2.84
Average Average Average
waiting turnaround normalized
time = 5 time = 10.5 turnaround
time = 1.86
Scheduling Algorithms
First Come First Served (FCFS)
Advantages
Simple to implement

Disadvantages
• With no pre-emption, it cant be used in multi-user or
real-time systems
• Shorter processes surfer longer waiting times due to
long processes
Scheduling Algorithms
Priority Scheduling
• The order of arrival of processes does not matter.
• This algorithm is pre-emptive, when a process with
higher priority has arrived late it will be executed first
according to its priority.
• Priorities are defined as a number associated with a
process.
• In most cases, lower numbers are considered as high
priorities but some systems implement a different
number scheme
• Disadvantages: Low priority processes may be starved
Scheduling Algorithms
Priority Scheduling
Example 3.2
Scheduling Algorithms
Priority Scheduling
Example 3.2 solution

Process Priority Arrival Execution Execution Waiting Turn Normalize


Time Time (x) Start Time Time around d Turn
Time (t) around
Time (x/t)
P1 2 0 5 0,6 4 9 1.8
P2 1 2 4 2 0 4 1
P3 3 3 7 9 6 13 1.85
P4 4 5 6 16 11 17 2.84
Average Average Average
waiting turnaroun normalize
time = d time = d
5.25 10.75 turnaroun
d time =
1.87
Scheduling Algorithms
Shortest Process Next (SPN)
• The process with the shortest execution time is executed
first.
• After a given instance of time, processes are compared
to get short processes.
• SPN is a non-pre-emptive scheduling algorithm. If a
process with the shortest execution time appears, it
cannot pre-empt a process with longer execution time.
• Disadvantage: Long processes can be starved
Scheduling Algorithms
Shortest Process Next (SPN)
Example 3.3
Scheduling Algorithms
Shortest Process Next (SPN)
Example 3.3 solution

Process Arrival Execution Execution Waiting Turn Normalized


Time Time (x) Start Time Time around Turn
Time (t) around
Time (x/t)
P1 0 5 0 0 5 1
P2 2 4 5 3 7 1.75
P3 3 7 9 12 19 2.7
P4 5 6 15 4 10 1.67
Average Average Average
waiting turnaround normalized
time = 4.75 time = turnaround
10.25 time = 1.78
Scheduling Algorithms
Shortest Remaining Time Next (SRN)
• This algorithm gives higher priority to shorter processes
as SPN, but it is a pre-emptive version of SPN.
• The process with the shortest execution time will always
pre-empt other processes.
• The criteria for pre-emption is based on the remaining
execution time of the processes.
Scheduling Algorithms
Shortest Remaining Time Next (SRN)
Example 3.4
Scheduling Algorithms
Shortest Remaining Time Next (SRN)
Example 3.4 solution

SRN

Process Arrival Time Execution Execution Waiting Turn around Norm. Turn
Time (x) Start Time Time Time (t) Around Time (x/t)

P1 0 9 0,13 12 21 2.34
P2 1 5 1,5 3 8 1.6
P3 2 3 2 0 3 1
P4 3 4 9 6 10 2.5
Av = 5.25 Av = 10.5 Av = 1.86
Scheduling Algorithms
Round Robin Scheduling (RR)
• The scheduling methods discussed so far do not
guarantee fair response times to all processes hence can
cause starvation to some processes
• With FCFS, tasks are processed according to the order of
their arrivals. A process arriving late may not get
response for a long time especially if earlier processes
are longer.
• With Priority scheduling, low priority processes can wait
for longer times before getting access to a processor
• With SPN/SRN, long processes may need to wait for a
long period before getting access to a processor
Scheduling Algorithms
Round Robin Scheduling (RR)
• With round robin algorithm, each arriving process gets
the same amount of time for execution.
• If each process gets the same processor time, neither
the short nor long process can suffer from starvation.
• To implement the round robin algorithm, the following
design issues are considered
i) The ready queue is maintained as a FIFO queue.
ii) A fixed time period is allotted to every arriving
process in the queue. This time period is known as time
slice or time quantum.
Scheduling Algorithms
Round Robin Scheduling (RR)
iii) The first arriving process is selected and dispatched
to the processor. But if it is not able to complete its
execution within its time slice, then an interrupt is
generated.
iv) As soon as the timer interrupt arrives, the running
process is stopped temporarily, and placed back in the
ready queue at the end of the queue.
v) The scheduler selects another process from the
queue and dispatches it to the processor. It is executed
until the allotted time slice expires.
vi) When a process finishes before the time slice
expires, the timer will stop and send the interrupt
signal, so that the next process can be scheduled.
Scheduling Algorithms
Round Robin Scheduling (RR)
Example 3.5
Scheduling Algorithms
Round Robin Scheduling (RR)
Example 3.5 solution

Process Arrival Executio Execution Waiting Turn Normalized


Time n Time Start Time Time around Turn
(x) Time (t) around
Time (x/t)
P1 0 9 0,8,15,18,20 12 21 2.34
P2 1 5 2,10,17 12 17 3.4
P3 2 3 4,12 8 11 3.67
P4 3 4 6,13 8 12 3
Average Average Average
waiting turnaround normalized
time = 10 time = turnaround
15.25 time = 3.10
Scheduling Algorithms
Round Robin Scheduling (RR)
• Optimum values of the time quantum must be selected
for good performance of the RR algorithm
• If the time quantum chosen is too large, the response
time of processes will increase
• if it is too small, context switch time will increase due to
the frequent process switching
• The quantum time should not be too long or too short,
the rule of thumb below can be used in selecting the
quantum time
i. 80% of the CPU bursts should be smaller than the
time quantum.
ii. Context switch time should be 10% or less of time
quantum.
Scheduling Algorithms
Highest Response Ratio Next Scheduling (HRRN)
• A metric called response ratio is used for making
scheduling decision
!"#$%&& '()% %*+,&%- (. '/% &0&'%)
response ratio =
1!2 '()% $#.&3)%- 40 '/% ,"#$%&&
• The response ratio indicates how much service a process
has received.
• Higher ratio indicates a process has received less service
and needs to get access to the CPU immediately.
• The system schedule a process with the highest
response ratio.
• The HRRN algorithm distributes the processor time more
fairly when compared to the simple round robin
algorithm
Scheduling Algorithms
Highest Response Ratio Next Scheduling (HRRN)
Example 3.6
Scheduling Algorithms
Highest Response Ratio Next Scheduling (HRRN)
Example 3.6 Solution
RR scenario

Process Arrival Time Execution Execution Start Waiting Turn around Normalized
Time (x) Time Time Time (t) Turn around
Time (x/t)

P1 0 3 0,1,8 6 9 3
P2 2 3 2,9,12 8 11 3.67
P3 3 2 3,4 0 2 1
P4 5 4 5,6,10,13 5 9 2.25
P5 7 2 7,11 3 5 2.5
Average Average Average
waiting time turnaround normalized
= 4.4 time = 7.2 turnaround
time = 2.484
Scheduling Algorithms
Highest Response Ratio Next Scheduling (HRRN)
Example 3.6 Solution
HRRN scenario
Response ratio calculation
Scheduling Algorithms
Highest Response Ratio Next Scheduling (HRRN)
Example 3.6 Solution
HRRN scenario

Process Arrival Time Execution Execution Start Waiting Turn around Normalized
Time (x) Time Time Time (t) Turn around
Time (x/t)

P1 0 3 0,1,4 2 5 1.67
P2 2 3 2,6,10 6 9 3
P3 3 2 3,8 4 6 3
P4 5 4 5,9,12,13 5 9 2.25
P5 7 2 7,11 3 5 2.5
Average Average Average
waiting time turnaround normalized
=4 time = 6.8 turnaround
time = 2.484
Scheduling Algorithms
Multi level Queue Scheduling
• The basic idea behind multiple queues is that all
processes are not of the same nature.
• Processes can be categorized into:
i. Interactive processes
ii. Non-interactive processes
iii. CPU-bound processes
iv. I/O-bound processes
v. Foreground processes
vi. Background processes

• Instead of a single ready queue storing all the processes,


there are multiple queues with different levels.
• Each queue store processes of the same category
• Queues are assigned different priorities
Scheduling Algorithms
Multi level Queue Scheduling
• Instead of a single ready queue storing all the processes,
there are multiple queues with different levels.
• Each queue store processes of the same category
• Scheduling is done
i. Among the queues.
ii. Between the processes in the selected queue.
• Interactive processes demand very
quick response time and have high
priority.
• I/O-bound processes are less
important hence stored in a second
queue.
• Background processes are of lowest
priority and are stored in a third
queue.
• Each queue has an absolute priority
over the one after it.
• If there is a process in the first
queue, any process of the second
queue cannot execute until the first
queue is empty.
Home Work
1. Go to https://teach-sim.com/tutorials/
2. Perform HW 2:
Investigating Process Scheduling 1
Investigating Process Scheduling 2
3. Upload the results (1 per group) in the LMS

You might also like