Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
103 views

OS - Lecture 04 - CPU Scheduling

Uploaded by

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

OS - Lecture 04 - CPU Scheduling

Uploaded by

Fahmi Ariffin2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Operating Systems

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
TW(P1) = 0 Process
(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
Example
Process Burst Time Arrival Waiting Turnaround
P1 24 0 0 24
P2 3 0 24 27
P3 4 0 27 31

FIFO Q P1 P2 P3

Process Q P1(24) P2(27) P3(31)

Avg waiting time = (0+24+27)/3 = 17ms

Avg turnaround time = (24+27+31)/3 = 27.33ms

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-14
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 48 0 0 48
P2 45 0 48 93
P3 35 3 93 128
P4 22 9 128 150

FIFO Q

Process Q P1 P2 P3 p4

Avg waiting time = (0+48+93+128)/4 = 67.25ms

Avg turnaround time = (48+93+128+150)/4 = 104.75ms

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-15
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 6 2 11 17
P2 2 5 21 23
P3 8 1 3 11
P4 3 0 0 3
P5 4 4 17 21

FIFO Q

Process Q P4 P3 P1 P5 p2

Avg waiting time = (11+21+3+0+17)/5 = 10.4ms

Avg turnaround time = (17+23+11+3+21)/5 = 15ms

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-16
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
Example
Process Burst Time Arrival Waiting Turnaround
P1 3 2 2 5
P2 4 0 5 9
P3 2 4 0 2
P4 4 5 9 13

Process Q
P3 P1 P2 p4

Avg waiting time = (2+5+0+9)/4 = 4ms

Avg turnaround time = (5+9+2+13)/4 = 7.25ms

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-20
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 48 0 102 150
P2 45 0 57 102
P3 35 3 22 57
P4 22 9 0 22

Process Q
P4 P3 P2 p1

Avg waiting time = (102+57+22+0)/4 = 45.25ms

Avg turnaround time = (150 + 102+57+22)/44 = 82.75ms

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-21
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 6 2 9 15
P2 2 5 0 2
P3 8 1 15 23
P4 3 0 2 5
P5 4 4 5 9

Process Q

P2 P4 P5 P1 p3

Avg waiting time = (9+0+15+2+5)/5 = 6.2ms

Avg turnaround time = (15+2+23+5+9)/5 = 10.8ms

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-22
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
Example
Process Burst Time Arrival Waiting Turnaround Priority
P1 3 2 6 9 3
P2 4 0 9 13 1
P3 2 4 0 2 4
P4 4 5 2 6 4

Process Q P3 P4 P1 p2

Avg waiting time = (6+9+0+2)/4 = 4.25ms

Avg turnaround time = (9+13+2+6)/4 = 7.5ms

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-26
Exercise
Process Burst Time Arrival Waiting Turnaroun Priority
d
P1 48 0 102 150 1
P2 45 0 22 67 3
P3 35 3 67 102 2
P4 22 9 0 22 4

Process Q
P4 P2 P3 P1

Avg waiting time = 47.75ms

Avg turnaround time = 85.25ms

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-27
Exercise
Process Burst Time Arrival Waiting Turnaroun Priority
d
P1 6 2 2 8 4
P2 2 5 0 2 5
P3 8 1 8 16 3
P4 3 0 16 19 1
P5 4 4 19 23 1

Process Q P2 P1 P3 P4 p5

Avg waiting time = 9ms

Avg turnaround time = 13.6ms

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-28
Starvation and Aging
 Starvation is associated with Priority
scheduling.
 Process may begin to run but needs because of
low priority.
 Computer heavily loaded with high priority
processes.
 Prevent low priority process can never get the
chance to use the CPU.

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-29
Process Burst Priority
1 10 90
2 11 12
3 7 1
4 12 2
5 8 3
6 6 87
7 3 9

1 6 2 7 5 4 3
0 10 16 27 30 38 50 57

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-30
Starvation and Aging
 Aging is a technique of gradually
increasing the priority of processes that
wait in the system for a long time.
 Example:
 Priority range: 0(low) -127(high)
 Processes that have low priority will be increase
in priority within a certain weight.
 1 for every 15 minutes.

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-31
Starvation and Aging

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-32
Drawbacks of Non-preemptive
Scheduling
1. It can lead to starvation especially in
real-time application.
2. Bugs in the machine can cause a
machine to freeze.
3. It can make real-time and priority
scheduling difficult.
4. Poor response time for processes
Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-33
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

Copyright © 2014 Pearson Education, Inc. Publishing as


Prentice Hall Slide 5-35
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
Example
Process Burst Time Arrival Waiting Turnaround
P1 3 2
P2 4 0
P3 2 4
P4 4 5

Time slice = 2ms


FIFO Q

Process Q

Avg waiting time =


Avg turnaround time =

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-38
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 8 0
P2 3 0
P3 1 3
P4 6 9

Time slice = 2ms


FIFO Q

Process Q

Avg waiting time =

Avg turnaround time =

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-39
Exercise
Process Burst Time Arrival Waiting Turnaround
P1 3 2
P2 2 5
P3 1 1
P4 3 0
P5 4 4
Time slice = 2ms
FIFO Q

Process Q

Avg waiting time =


Avg turnaround time =

Copyright © 2014 Pearson Education, Inc. Publishing as Prentice Hall Slide 5-40
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 (MLQ)
 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
Example
Process Arrival Burst Queue Waiting Turnaround
Number Time time
1 0 4 1
2 0 3 1
3 0 8 2
4 10 5 1

Queue 1: Round Robin (Time slice = 2) Q1 > Q2


Queue 2: FCFS
Example
Process Arrival Burst Queue Waiting Turnaround
Number Time Time
1 0 4 0
2 0 3 1
3 0 5 0

Queue Number 0: FCFS Queue 0 = System Process


Queue Number 1: Round Robin (2) Queue 1 = Student Process
Queue 0 > Queue 1
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.
Q1 and Q2 are Round Robin (Time slice 4
& 8).
Q3 is FCFS.
Implementation of MLFQ:
1. Executes Q1.
2. If process in Q1 completes its process
in 4 units, then priority remain.
3. If the processes does not complete in
4 units, then shift to Q2.
4. The same applies to Q2 but in 8 units.
5. If a process in Q2 does not complete
in 8 units, then shift to Q3.
6. A process in a lower priority can only
execute when there are no process in
higher priority.
7. A process in lower priority can be
interrupted by a process arriving in
higher priority.

Copyright © 2014 Pearson Education, Inc. Slide 5-


Publishing as Prentice Hall 47
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
Next Session

Chapter 4:
OS Performance

You might also like