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

12_Operating Sytems-CPU Scheduling

Uploaded by

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

12_Operating Sytems-CPU Scheduling

Uploaded by

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

Operating Systems &

Computer Architecture
CT049-3-1-OS&CA
Ver: VE

CPU Scheduling
Learning Outcomes

At the end of this section, YOU should be able to:

• State the Aims of CPU Scheduling


• Perform calculations using the various scheduling Algorithms available

Module Code & Module Title Slide Title SLIDE 2


Topics we will cover
• CPU scheduling
• Preemptive Scheduling Algorithms
• Non-preemptive Scheduling Algorithms
• Calculation using Preemptive and Non-preemptive Scheduling
Algorithms

Module Code & Module Title Slide Title SLIDE 3


CPU Scheduling

• A CPU scheduler is tasked with choosing which process to run first from
the ready queue.
• A scheduling algorithm is used to choose the next process.
• Scheduling is a fundamental function of an operating system.
• Scheduling is done to ensure that the CPU is not idle.

Module Code & Module Title Slide Title SLIDE 4


Aims of Scheduling

• fairness
– to make sure all processes get a fair share of the
CPU time.
– avoid starvation.
• efficiency
– maximise CPU utilisation and keep the CPU busy
close to 100% of the time.
• response time
– consistent response time.
– minimise response time.

Module Code & Module Title Slide Title SLIDE 5


Aims of Scheduling

• turnaround time
– minimise time between submission and job completion.
– minimise output time.
• throughput
– maximise number of job completed within a given time period.

Module Code & Module Title Slide Title SLIDE 6


Preemptive Scheduling

• Allows for running processes to be temporarily suspended.


• Processes releases CPU upon receiving a command.
• Preemptive scheduling algorithms:-
– round robin
– multilevel queue
– multilevel feedback queue

Module Code & Module Title Slide Title SLIDE 7


Non-preemptive Scheduling

• Processes release the CPU only after completion.


• Processes releases CPU voluntarily.
• Non-preemptive scheduling algorithms:-
– first come first serve (FCFS) or first in first out (FIFO)
– shortest job first (SJF)
– priority

Module Code & Module Title Slide Title SLIDE 8


Quick Review Questions

1. Why is CPU scheduling important?


2. What is the difference between preemptive and non-preemptive
algorithms?
3. Give 2 examples each for a preemptive and non-preemptive
algorithms.

Module Code & Module Title Slide Title SLIDE 9


Calculation Keywords

• CPU utilisation
• Throughput
• Turnaround time
– Average turnaround time
• Waiting time
– Average waiting time
• Response time
• Burst time

Module Code & Module Title Slide Title SLIDE 10


Round Robin

• Oldest, simplest, fairest and most widely


used.
• Designed for time-sharing systems.
• Time quantum/time slice is defined for
each process.
• The ready queue is treated as a circular
queue.
• To implement round robin, the ready queue
is kept as a first in first out queue.

Module Code & Module Title Slide Title SLIDE 11


Round-robin
Step 1:- Draw a Gantt Chart to represent timing for all processes

P1 P2 P3 P4 P5 P1 P5 P1 P5 P1 P1
0 2 3 5 6 8 10 12 14 15 17 19
Step 2:- Calculate waiting times and average waiting time
Time Slice = 2
TW(P1) = (0+6+2+1)=9 Milliseconds
TW(P2) = 2 Burst Time
Process (Milliseconds)
TW(P3) = 3
TW(P4) = 5 P1 10
TW(P5) = (6+2+2)=10 P2 1
TW(average)=(9+2+3+5+10)/5 P3 2
P4 1
=5.8 Milliseconds
Module Code & Module Title
P5
Slide Title
5 SLIDE 12
Round-robin

Step 3:- Calculate turnaround times and average turnaround time


TT = Tw+TB

P1 P2 P3 P4 P5 P1 P5 P1 P5 P1 P1
0 2 3 5 6 8 10 12 14 15 17 19

TT(P1)=(9+10)=19 Time Slice = 2 Milliseconds


TT(P2)=(2+1) = 3 Burst Time
TT(P3)=(3+2) = 5
Process (Milliseconds
)
TT(P4)=(5+1) = 6 P1 10
TT(P5)=(10+5)=15 P2 1
Average turnaround time is (19+3+5+6+15)/5 = P3 2
(48/5)=9.6 Milliseconds P4 1
P5 5
Module Code & Module Title Slide Title SLIDE 13
Round Robin

• Average waiting time is usually long.


• Performance depends on the size of the time
quantum (10 – 100 milliseconds).
• Process switch requires time, while the time
quantum clock is already running.
• Time quantum which is set too short would
result in too many process switches and this
reduces CPU efficiency.
• Time quantum which is too long would cause
poor response to short interactive request.

Module Code & Module Title Slide Title SLIDE 14


Multilevel Queue

• Classifies processes into different groups.


• The ready queue is separated into several
separate queues.
• Each process is permanently assigned to one
queue based on process priority, size or process
type.
• Each queue would have its own scheduling
algorithm.
• Each queue has priority over lower level queues.
• Time slices can be allocated to queues.

Module Code & Module Title Slide Title SLIDE 15


Multilevel Queue

highest
priority
System Processes

Interactive Processes

Interactive Editing Processes

Batch Processes

Student Processes
lowest
priority
Module Code & Module Title Slide Title SLIDE 16
Multilevel Feedback Queue

• Processes are allowed to move between queues.


• Processes are separated by CPU burst time.
• If a process utilises too much CPU time it will be moved to a lower
priority queue.
• If a process is starved of CPU time it will be moved to a higher level
priority queue.

Module Code & Module Title Slide Title SLIDE 17


Multilevel Feedback Queue

quantum=8

quantum=16

FCFS

Module Code & Module Title Slide Title SLIDE 18


Quick Review Questions

1. How does the round robin algorithm work?

2. What is the difference between the multilevel queue and multilevel


feedback queue?

Module Code & Module Title Slide Title SLIDE 19


Follow Up Assignment

1. List down some features of the round robin scheduling algorithm

2. What is turnaround time?

3. Why are priorities used?

Module Code & Module Title Slide Title SLIDE 20


FIFO / FCFS

• The simplest CPU scheduling algorithm.


• The process that requests the CPU first is
allocated the CPU first.
• The implementation of the FIFO policy is easily
managed with a FIFO queue.
• Average waiting time for FIFO is quite long.
• Once the CPU has been allocated the process,
the process keeps the CPU until termination or
by requesting for I/O.

Module Code & Module Title Slide Title SLIDE 21


FIFO / FCFS
Step 1:- Draw Gantt Chart to represent timing for all processes
P1 P2 P3 P4 P5
0 10 11 1314 19
Step 2:- Calculate waiting times and average waiting time
Burst Time
TW(P1) = 0 Process (Millisecond
TW(P2) = 10 s)
TW(P3) = 11 P1 10
TW(P4) = 13
P2 1
TW(P5) = 14
P3 2
TW(average) = (0+10+11+13+14)/5
= 9.6 Milliseconds P4 1
P5 5
Module Code & Module Title Slide Title SLIDE 22
FIFO / FCFS
Step 3:- Calculate turnaround times and average turnaround time
TT = Tw+TB
P1 P2 P3 P4 P5
0 10 11 1314 19
TT(P1) = (0+10) = 10
TT(P2) = (10+1) = 11 Burst Time
Process
TT(P3) = (11+2) = 13 (Milliseconds)
TT(P4) = (13+1) = 14 P1 10
P2 1
TT(P5) = (14+5) = 19
Average turnaround time is P3 2
(10+11+13+14+19)/5 = (67/5) P4 1
=13.4 Milliseconds P5 5

Module Code & Module Title Slide Title SLIDE 23


Shortest Job First (SJF)

• Scheduling is done by examining the length of the


next CPU burst of a process.
• If the CPU is free, the next process with the smallest
next CPU burst is assigned.
• If two processes have the same CPU burst, FIFO is
used to break the tie.
• The difficulty with SJF is to determine the length of the
next process.
• The advantage of this algorithm is that it is optimal by
providing the minimum average waiting time.

Module Code & Module Title Slide Title SLIDE 24


Shortest Job First (SJF)
Step 1:- Draw Gantt Chart to represent the timing for all processes
P2 P4 P3 P5 P1
0 1 2 4 9 19

Step 2:- Calculate waiting times and average waiting time

TW(P1) = 9 Burst Time


Process
TW(P2) = 0 (Milliseconds)
TW(P3) = 2
P1 10
TW(P4) = 1
P2 1
TW(P5) = 4
TW(average) = (9+0+2+1+4)/5 P3 2
= 3.2 Milliseconds P4 1
P5 5

Module Code & Module Title Slide Title SLIDE 25


Shortest Job First (SJF)
Step 3: Calculate turnaround times and average turnaround time
TT = Tw+TB
P2 P4 P3 P5 P1
0 1 2 4 9 19

TT(P1) = (9+10) = 19
Burst Time
Process
TT(P2) = (0+1) = 1 (Milliseconds)

TT(P3) = (2+2) = 4
P1 10
TT(P4) = (1+1) = 2 P2 1
TT(P5) = (4+5) = 9 P3 2

Average turnaround time is P4 1

(19+1+4+2+9)/5 = (35/5) P5 5
=7 Milliseconds
Module Code & Module Title Slide Title SLIDE 26
Priority

• Priority is associated with each process.


• The CPU is allocated the job with highest priority.
• Equal priority processes are scheduled using FIFO.
• Priority can be high or low; however 0 can mean high
priority.
• Can be preemptive or non-preemptive.
• A problem with priority algorithm is starvation.
• Aging is a technique to gradually increase a processes
priority.
Module Code & Module Title Slide Title SLIDE 27
Priority

Step 1:- Draw Gantt Chart to represent timing for all processes
P4 P1 P3 P5 P2
0 1 11 13 18 19

Step 2:- Calculate waiting time and average waiting time


TW(P1) = 1
TW(P2) = 18 Burst Time
TW(P3) = 11 Process Priority
(ms)
TW(P4) = 0 P1 10 3
TW(P5) = 13 P2 1 1
TW(average) = (1+18+11+0+13)/5 P3 2 3
= 8.6 Milliseconds P4 1 4
P5 5 2

Module Code & Module Title Slide Title SLIDE 28


Priority
Step 3:- Calculate turnaround times and average turnaround time
TT = Tw+TB
P4 P1 P3 P5 P2
0 1 11 13 18 19

TT(P1) = (1+10) = 11
TT(P2) = (18+1) = 19 Burst Time
Process Priority
TT(P3) = (11+2) = 13 (ms)

TT(P4) = (0+1) = 1 P1 10 3
P2 1 1
TT(P5) = (13+5) = 18 P3 2 3
Average turn around time is
P4 1 4
(11+19+13+1+18)/5 = (62/5) P5 5 2
=12.4 Milliseconds
Module Code & Module Title Slide Title SLIDE 29
Quick Review Questions

1. Name three preemptive scheduling algorithms.


2. Which of the three is the easiest to implement?

Module Code & Module Title Slide Title SLIDE 30


Follow Up Assignment

1. FIFO on its own is a scheduling algorithm, however FIFO is also used


in other scheduling algorithms. Name these algorithms.
2. Which non-preemptive algorithm is optimal and why?

Module Code & Module Title Slide Title SLIDE 31


Summary of Main Teaching Points

• CPU scheduling is used to select a process from the


ready queue and allocate this process CPU time.
• Scheduling algorithms can be pre-emptive or non-pre-
emptive.
• Scheduling algorithms aim to achieve fairness,
efficiency, maximising throughput and minimising
turnaround time.
• Round robin, multilevel queues and multilevel
feedback queues are examples of pre-emptive
algorithms.

Module Code & Module Title Slide Title SLIDE 32


Summary of Main Teaching Points

• Non-preemptive algorithms are FIFO, SJF and priority.


• SJF algorithm has a shorter average waiting time.
• Using priority scheduling may result in some processes experiencing
starvation; aging is a method used to overcome this.

Module Code & Module Title Slide Title SLIDE 33


END

Q&A
Module Code & Module Title Slide Title SLIDE 34

You might also like