Ch5 CPU Scheduling
Ch5 CPU Scheduling
!
Operating System Concepts – 8th Edition, !
Silberschatz, Galvin and Gagne ©2009
Chapter 5: CPU Scheduling !
• Basic Concepts!
• Scheduling Criteria !
• Scheduling Algorithms!
• Thread Scheduling!
• Multiple-Processor Scheduling!
• Linux Example!
!
Operating System Concepts – 8th Edition !
2 !
Silberschatz, Galvin and Gagne ©2009
Objectives
!
!
Operating System Concepts – 8th Edition !
3 !
Silberschatz, Galvin and Gagne ©2009
Basic Concepts
!
!
Operating System Concepts – 8th Edition !
4 !
Silberschatz, Galvin and Gagne ©2009
Alternating Sequence of CPU And I/O Bursts!
!
Operating System Concepts – 8th Edition !
5 !
Silberschatz, Galvin and Gagne ©2009
Basic Concepts
!
!
!
!
Operating System Concepts – 8th Edition !
6 !
Silberschatz, Galvin and Gagne ©2009
Histogram of CPU-burst Times !
!
Operating System Concepts – 8th Edition !
7 !
Silberschatz, Galvin and Gagne ©2009
CPU Scheduler
!
!
Operating System Concepts – 8th Edition !
8 !
Silberschatz, Galvin and Gagne ©2009
CPU Scheduler
!
!
Operating System Concepts – 8th Edition !
9 !
Silberschatz, Galvin and Gagne ©2009
Preemptive scheduling
!
!
Operating System Concepts – 8th Edition !
10 !
Silberschatz, Galvin and Gagne ©2009
Dispatcher
!
!
Operating System Concepts – 8th Edition !
11 !
Silberschatz, Galvin and Gagne ©2009
Scheduling Criteria
!
!
Operating System Concepts – 8th Edition !
13 !
Silberschatz, Galvin and Gagne ©2009
First-Come, First-Served (FCFS) Scheduling!
• Jobs are scheduled in order of arrival!
• When a process enters the ready queue, its PCB is
linked onto the tail of the queue.!
• When the CPU is free, it is allocated to the process at
the head of the queue (the running process is then
removed from the queue).!
• Disadvantages:!
• Non-preemptive : once the CPU is allocated to a
process, the process keeps the CPU until it releases it,
either by terminating or requesting I/O.!
• The average waiting time is often quite long.!
!
! !!
!
Operating System Concepts – 8th Edition !
14 !
Silberschatz, Galvin and Gagne ©2009
example !
!
Operating System Concepts – 8th Edition !
15 !
Silberschatz, Galvin and Gagne ©2009
FCFS Scheduling (Cont) !
! 0! 3! 6! 30!
• Waiting time for P1 = 6; P2 = 0; P3 = 3!
• Average waiting time: (6 + 0 + 3)/3 = 3!
• Much better than previous case!
• Convoy effect as short processes go behind long process
àlower CPU and device utilization.!
Ready Queue!
CPU!
Running
P1!
I/ O Queue!
I/O device ! Running!
P5! P4! P3! P2!
!
Operating System Concepts – 8th Edition !
Silberschatz, Galvin and Gagne ©2009
Convoy effect: example !
During this time, all the other processes will finish their I/0 and will move into
the ready queue, waiting for the CPU !
Ready Queue!
CPU!
!
Operating System Concepts – 8th Edition !
Silberschatz, Galvin and Gagne ©2009
Convoy effect: example !
Eventually, the CPU-bound process finishes its CPU burst and moves to an
I/0 device. !
Ready Queue!
CPU!
!
Operating System Concepts – 8th Edition !
Silberschatz, Galvin and Gagne ©2009
Convoy effect: example!
All the I/O-bound processes, which have short CPU bursts, execute quickly
and move back to the I/0 queues. !
Ready Queue!
CPU!
At this point, the CPU sits idle. !
Idle!
I/ O Queue!
P5! P4! P3! P2! I/O device ! Running!
P1!
The CPU-bound process will then move back to the ready queue and be
allocated the CPU. !
Again, all the I/O processes end up waiting in the ready queue until the CPU-
bound process is done. !
CPU!
Ready Queue!
Running
P1!
I/ O Queue!
I/O device ! Running!
P5! P4! P3! P2!
!
Operating System Concepts – 8th Edition !
Silberschatz, Galvin and Gagne ©2009
Shortest-Job-First (SJF) Scheduling !
!
Operating System Concepts – 8th Edition !
22 !
Silberschatz, Galvin and Gagne ©2009
Examples of SJF !
Example1:!
! !Process!Arrival Time !Burst Time!
! ! P1 !0.0 !6!
! ! P2 !2.0 !8!
! ! P3 !4.0 !7!
! ! P4 !5.0 !3!
• SJF scheduling chart!
P4! P1! P3! P2!
!
0! 3! 9! 16! 24!
• Average waiting time = (3 + 16 + 9 + 0) / 4 = 7!
• Compare with FCFS AWT=(0+6+14+21)/4=10.25!
0! 2! 4! 5! 7! 8! 12! 16!
P1‘s wating time = 0!
P1(7)!
P2(4)! P2‘s wating time = 6!
P3‘s wating time = 3!
P3(1)!
P4‘s wating time = 7!
P4(4)!
Example3:!
! ! !Process !Arrival Time Burst Time !!
! ! ! P1 ! 0 ! !7!
! ! ! P2 ! 2 ! !4!
! ! ! P3 ! 4 ! !1!
! ! P4 ! 5 ! 4!
•Preemptive SJF(SRTF)!
Average waiting time = (9 + 1 + 0 +2)/4 = 3!
0! 2! 4! 5! 7! 11! 16!
P1(5)! P1‘s wating time = 9!
P1(7)!
P2(4)! P2(2)! P2‘s wating time = 1!
P3‘s wating time = 0!
P3(1)!
P4‘s wating time = 2!
P4(4)!
!
Operating System Concepts – 8th Edition !
26 !
Silberschatz, Galvin and Gagne ©2009
Prediction of the Length of the Next CPU Burst!
!
Operating System Concepts – 8th Edition !
27 !
Silberschatz, Galvin and Gagne ©2009
Priority Scheduling
!
!
Operating System Concepts – 8th Edition !
28 !
Silberschatz, Galvin and Gagne ©2009
Priority Scheduling
!
!
Operating System Concepts – 8th Edition !
29 !
Silberschatz, Galvin and Gagne ©2009
Priority Scheduling !
!
Operating System Concepts – 8th Edition !
30 !
Silberschatz, Galvin and Gagne ©2009
Priority Scheduling !
Example:!
Process arrival time Burst length Priority!
! ! P1 0 10 3!
P2 0 1 1!
P3 0 2 4!
P4 0 1 5!
P5 3 5 2!
•Gantt chart: Non-preemptive priority scheduling!
! P2! P1! P5! P3! P4!
0 1 11 16 18 19!
•Gantt chart: Preemptive priority scheduling!
!
! P2! P1! P5! P1! P3! P4!
0 1 3 8 16 18 19!
!
Operating System Concepts – 8th Edition !
31 !
Silberschatz, Galvin and Gagne ©2009
Round Robin (RR)
!
!
Operating System Concepts – 8th Edition !
32 !
Silberschatz, Galvin and Gagne ©2009
Round Robin (RR)
!
!
Operating System Concepts – 8th Edition !
33 !
Silberschatz, Galvin and Gagne ©2009
Round Robin (RR) !
Example1:!
Time quantum = 4!
!
! !Process !Burst Time!
! !P1 !24!
! ! P2 ! 3!
! ! P3 !3!
!
P1! P2! P3! P1! P1! P1! P1! P1!
!
0! 4! 7! 10! 14! 18! 22! 26! 30!
!
• AWT(6(10-4)+4+7)/3 = 5.66!
• Example2:!
• Time quantum = 20!
! !Process! Burst Time ! Wait Time!
! ! P1 ! !53 ! !57 +24 = 81!
! ! P2 ! !17 ! !20!
! ! P3 ! !68 ! !37 + 40 + 17= 94!
! ! P4 ! !24 ! !57 + 40 = 97!
P1! P2! P3! P4! P1! P3! P4! P1! P3! P3!
0! 20! 37! 57! 77! 97! 117! 121! 134! 154! 162!
P1(53)! 57!
P1(33)! 24!P1(13)!
P2(17)!20!
P3(68)! 37! 40!P3(48)! 17!
P3(28)! P3(8)!
!
Operating System Concepts – 8th Edition !
37 !
Silberschatz, Galvin and Gagne ©2009
Turnaround Time Varies With The Time Quantum!
!
Operating System Concepts – 8th Edition !
38 !
Silberschatz, Galvin and Gagne ©2009
Multilevel Queue
!
!
Operating System Concepts – 8th Edition !
39 !
Silberschatz, Galvin and Gagne ©2009
Multilevel Queue
!
!
Operating System Concepts – 8th Edition !
40 !
Silberschatz, Galvin and Gagne ©2009
Multilevel Queue Scheduling !
!
Operating System Concepts – 8th Edition !
41 !
Silberschatz, Galvin and Gagne ©2009
Multilevel Feedback Queue !
!
Operating System Concepts – 8th Edition !
42 !
Silberschatz, Galvin and Gagne ©2009
Multilevel Feedback Queue !
!
Operating System Concepts – 8th Edition !
43 !
Silberschatz, Galvin and Gagne ©2009
Example of Multilevel Feedback Queue !
• Three queues: !
• Q0 – RR with time quantum 8 milliseconds!
• Q1 – RR time quantum 16 milliseconds!
• Q2 – FCFS!
• Scheduling!
• A new job enters queue Q0 which is served FCFS. When it gains CPU,
job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is
moved to queue Q1.!
• At Q1 job is again served FCFS and receives 16 additional milliseconds.
If it still does not complete, it is preempted and moved to queue Q2.!
• AT Q2 job is served FCFS only when queue 0 and queue 1 are empty.!
!
Operating System Concepts – 8th Edition !
44 !
Silberschatz, Galvin and Gagne ©2009
Multilevel Feedback Queues !
!
Operating System Concepts – 8th Edition !
45 !
Silberschatz, Galvin and Gagne ©2009
Thread Scheduling
!
!
Operating System Concepts – 8th Edition !
46 !
Silberschatz, Galvin and Gagne ©2009
Thread Scheduling !
!
Operating System Concepts – 8th Edition !
47 !
Silberschatz, Galvin and Gagne ©2009
Multiple-Processor Scheduling !
!
Operating System Concepts – 8th Edition !
48 !
Silberschatz, Galvin and Gagne ©2009
Multiple-Processor Scheduling !
!
Operating System Concepts – 8th Edition !
49 !
Silberschatz, Galvin and Gagne ©2009
Linux Scheduling!
!
Operating System Concepts – 8th Edition !
50 !
Silberschatz, Galvin and Gagne ©2009
Linux Scheduling
!
!
Operating System Concepts – 8th Edition !
51 !
Silberschatz, Galvin and Gagne ©2009
List of Tasks Indexed According to Priorities!
• The scheduler chooses the task with the highest priority
from the active array for execution on the CPU. !
• When the active array is empty à the 2 arrays are
exchanged (the expired array becomes the active array,
and vice versa).!
!
Operating System Concepts – 8th Edition !
52 !
Silberschatz, Galvin and Gagne ©2009
Algorithm Evaluation !
• Deterministic modeling!
• takes a particular predetermined workload and defines the
performance of each algorithm for that workload!
• More examples P: 214!
• Simple and fast!
• Requires exact numbers for input and its answers apply only for
those data!
• Queueing models!
• rate at which new processes arrive, ratio of CPU bursts to I/O
times, distribution of CPU burst times and I/O burst times can
be measured and then approximated or estimated!
• result is a mathematical formula describing it!
• From these it is possible to compute the average throughput,
utilization, waiting time, and so on!
• difficult to work!
!
Operating System Concepts – 8th Edition !
53 !
Silberschatz, Galvin and Gagne ©2009
Algorithm Evaluation !
• Simulations!
• run computer simulations of the different proposed algorithms !
• data to drive the simulation can be randomly generated !
• better alternative when possible is to generate trace tapes!
• expensive!
• Implementation!
• The only completely accurate way to evaluate a scheduling algorithm is to
code it up, put it in the operating system, and see how it works.!
• high cost (coding and user reaction)!
!
Operating System Concepts – 8th Edition !
54 !
Silberschatz, Galvin and Gagne ©2009
Simulations
!
!
Operating System Concepts – 8th Edition !
55 !
Silberschatz, Galvin and Gagne ©2009
Conclusion
!
!
Operating System Concepts – 8th Edition !
56 !
Silberschatz, Galvin and Gagne ©2009
End of Chapter 5
!
!
Operating System Concepts – 8th Edition, !
Silberschatz, Galvin and Gagne ©2009