You are on page 1of 15

Chapter 5b: CPU Scheduling

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Scheduling Algorithm

● First –come, First-serve (FCFS: Non-Preemptive)


● Shortest-Job-First Scheduling (SJF: Non-Preemptive)
● Shortest-Remaining-Time-First (Preemptive)
● Round-Robin Scheduling (RR: Preemptive)

Operating System Concepts – 9th Edition 5.2 Silberschatz, Galvin and Gagne ©2013
First- Come, First-Served (FCFS) Scheduling

● Consider the following three processes and their burst time

Process Burst Time


P1 24
P2 3
P3 3

● Suppose that the processes arrive in the order: P1 , P2 , P3


● We use Gantt Chart to illustrate a particular schedule

● Waiting time for P1 = 0; P2 = 24; P3 = 27


● Average waiting time: (0 + 24 + 27)/3 = 17

Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne ©2013
FCFS Scheduling (Cont.)

● Suppose that the processes arrive in the order:


P2 , P3 , P1
● The Gantt chart for the schedule is:

● Waiting time for P1 = 6; P2 = 0; P3 = 3


● Average waiting time: (6 + 0 + 3)/3 = 3
● Much better than previous case
● Convoy effect - short process behind long process
● Consider one CPU-bound and many I/O-bound processes

Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne ©2013
Shortest-Job-First (SJF)

● SJF – an algorithm in which the process having the smallest


execution time is chosen for the next execution. This
scheduling method can be preemptive or non-preemptive.
● SJF is optimal – gives minimum average waiting time for a
given set of processes
● How do we know what is the length of the next CPU
request
● Could ask the user
4 what if the user lies?

Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne ©2013
Example of SJF

■ Consider the following four processes and their burst time

Process Burst Time


P1 6
P2 8
P3 7
P4 3

● SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 23

● Average waiting time = (3 + 16 + 9 + 0) / 4 = 7

Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne ©2013
Shortest-Remaining-Time-First (SRTF)
● Preemptive version of SJF is called shortest-remaining-time-first

Process Arrival Time Burst Time


P1 0 8
P2 1 4
P3 2 9
P4 3 5
● Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

● Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 ms

Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne ©2013
SRTF/SJF (Preemptive): Question?

■ Consider the following four processes and their burst time

Process Arrival Time Burst Time


P1 2 6
P2 5 2
P3 1 8
P4 0 3
P5 4 4

● Create SRTF/SJF(Preemptive) Scheduling chart?


● Calculate Average Turn-Around Time?
● Calculate Average waiting time?

Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne ©2013
SRTF/SJF (Preemptive) - Solution
Process Arrival Time Burst Time Turn-Around Waiting Time
P1 2 6 15-2=13 13-6=7
P2 5 2 2 0
P3 1 8 22 14
P4 0 3 3 0
P5 4 4 6 2

turn-around time = CT - AT
Waiting time = TAT - BT

● SRTF/SJF(Preemptive) scheduling chart

P4 P1 P5 P2 P5 P1 P3
0 3 4 5 7 10 15 23

● Average waiting time = (7 + 0 + 14 + 0 + 2) / 5 = 26 / 5 = 4.6


● Average turn-around time = (13 + 2 + 22 + 3 + 6) / 5 = 9.2

Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne ©2013
Round Robin (RR)

● Each process gets a small unit of CPU time (time quantum q).
After this time has elapsed, the process is preempted and
added to the end of the ready queue.
● If there are N processes in the ready queue and the time
quantum is q, then each process gets 1/N of the CPU time in
chunks of at most q time units at once. No process waits
more than (N-1)q time units.
● Timer interrupts every quantum to schedule next process
● Performance
● q large ⇒ FIFO
● q small ⇒ q must be large with respect to context switch,
otherwise overhead is too high

Operating System Concepts – 9th Edition 5.10 Silberschatz, Galvin and Gagne ©2013
Example of RR with Time Quantum = 4

■ Consider the following three processes and their burst time

Process Burst Time


P1 24
P2 3
P3 3

● The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

● The average waiting time under the RR policy is often longer


● Typically, higher average turnaround than SJF, but better response

Operating System Concepts – 9th Edition 5.11 Silberschatz, Galvin and Gagne ©2013
Round Robin (TQ = 2) : Question?

■ Consider the following four processes and their burst time

Process Arrival Time Burst Time


P1 0 5
P2 1 4
P3 2 2
P4 4 1

● Create Round Robin (Preemptive) Scheduling chart?


● Calculate Average Turn-Around Time?
● Calculate Average waiting time?

Operating System Concepts – 9th Edition 5.12 Silberschatz, Galvin and Gagne ©2013
Round Robin (TQ = 2) - Solution
Process Arrival Time Burst Time Turn-Around Waiting Time
P1 0 5 12-0=12 12-5=7
P2 1 4 10 6
P3 2 2 4 2
P4 4 1 5 4

P1 P2 P3 P1 P4 P2 P1
Ready Queue:

turn-around time = CT - AT
● Round Robin (Preemptive) scheduling chart: Waiting time = TAT - BT

P1 P2 P3 P1 P4 P2 P1
0 2 4 6 8 9 11 12

● Average waiting time = (7 + 6 + 2 + 4) / 4 = 19 / 4 = 4.75


● Average turn-around time = (12 + 10 + 4 + 5) / 4 = 41 / 4 = 10.25.

Operating System Concepts – 9th Edition 5.13 Silberschatz, Galvin and Gagne ©2013
SRTF/RR: Class Activity?

■ Consider the following four processes and their burst time

Process Arrival Time Burst Time


P1 0 7
P2 1 5
P3 2 3
P4 3 1
P5 4 2
P6 5 1

● Create SRTF/SJF(Preemptive) Scheduling chart?


● Create RR(Preemptive) Scheduling chart with TQ = 2 ?
● Calculate Average Turn-Around Time?
● Calculate Average waiting time?

Operating System Concepts – 9th Edition 5.14 Silberschatz, Galvin and Gagne ©2013
End of Chapter 5

Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013

You might also like