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

OSS - Lecture 02-2 - CPU Scheduling

CPU scheduling algorithms are used to determine which process runs next on the CPU. Preemptive algorithms allow running processes to be temporarily suspended, while non-preemptive algorithms only allow voluntary release of the CPU. Common algorithms include first come first serve (non-preemptive), shortest job first (preemptive), priority scheduling (can be preemptive or non-preemptive), and round robin (preemptive with time slices). Key metrics for evaluating algorithms include waiting time, turnaround time, throughput, and CPU utilization.

Uploaded by

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

OSS - Lecture 02-2 - CPU Scheduling

CPU scheduling algorithms are used to determine which process runs next on the CPU. Preemptive algorithms allow running processes to be temporarily suspended, while non-preemptive algorithms only allow voluntary release of the CPU. Common algorithms include first come first serve (non-preemptive), shortest job first (preemptive), priority scheduling (can be preemptive or non-preemptive), and round robin (preemptive with time slices). Key metrics for evaluating algorithms include waiting time, turnaround time, throughput, and CPU utilization.

Uploaded by

Afiq Aiman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Operating System Security

IKB20103

CPU Scheduling
Topic & Structure of the lesson
 CPU scheduling
 Preemptive scheduling algorithms
 Non-preemptive scheduling algorithms
 Calculation using preemptive scheduling
algorithms
Learning Outcomes
 At the end of this session you should be
able to:-
 explain the importance of CPU scheduling.
 distinguish between preemptive and non-
preemptive algorithms.
 use algorithms to calculate turnaround time.
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.
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.
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.
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
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
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.
Calculation Keywords
 CPU utilisation
 Throughput
 Turnaround time
 Average turnaround time

 Waiting time
 Average waiting time
 Response time
 Burst time
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.
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
Process
TW(P1) = 0 (Milliseconds)
TW(P2) = 10 P1 10
TW(P3) = 11 P2 1
TW(P4) = 13 P3 2
TW(P5) = 14 P4 1
TW(average) = (0+10+11+13+14)/5 P5 5
= 9.6 Milliseconds
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
P3 2
Average turnaround time is P4 1
(10+11+13+14+19)/5 = (67/5) P5 5
=13.4 Milliseconds
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.
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
(Milliseconds)
TW(P2) = 0
TW(P3) = 2 P1 10
TW(P4) = 1 P2 1
TW(P5) = 4 P3 2
TW(average) = (9+0+2+1+4)/5 P4 1
= 3.2 Milliseconds P5 5
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
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.
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
Process Priority
TW(P3) = 11 (ms)
TW(P4) = 0 P1 10 3
TW(P5) = 13 P2 1 1

TW(average) = (1+18+11+0+13)/5 P3 2 3
P4 1 4
= 8.6 Milliseconds P5 5 2
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
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.
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 Milliseconds
TW(P1) = (0+6+2+1)=9
Burst Time
TW(P2) = 2 Process
(Milliseconds)
TW(P3) = 3 P1 10
TW(P4) = 5 P2 1
TW(P5) = (6+2+2)=10 P3 2
TW(average)=(9+2+3+5+10)/5 P4 1
=5.8 Milliseconds P5 5
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
Process
TT(P3)=(3+2) = 5 (Milliseconds)
TT(P4)=(5+1) = 6 P1 10
TT(P5)=(10+5)=15 P2 1

Average turnaround time is P3 2


(19+3+5+6+15)/5 = (48/5)=9.6 P4 1
Milliseconds P5 5
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.
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.
Multilevel Queue
highest priority
System Processes

Interactive Processes

Interactive Editing Processes

Batch Processes

Student Processes
lowest priority
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.
Multilevel Feedback Queue

quantum=8

quantum=16

FCFS
Quick Review Questions
1. How does the round robin algorithm work?
2. What is the difference between the pre-
emptive or non-pre-emptive algorithms?
3. Which scheduling algorithms may cause
starvation?
4. Generally, which algorithms provide the
shortest: waiting time and turnaround time?
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 (preemptive or non-preemptive) aim
to achieve fairness, efficiency, maximising throughput and
minimising turnaround time.
 Round robin, multilevel queues and multilevel feedback
queues are examples of preemptive algorithms.
 Non-preemptive algorithms are FIFO, SJF and priority.
 SJF algorithm has a shorter average waiting time.
 Using some scheduling may result starvation; aging is a
method used to overcome this.
Q&A

You might also like