chapter 5: cpu scheduling - operating system · chapter 5: cpu scheduling! ......

57
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 5: CPU Scheduling

Upload: dangdan

Post on 18-May-2018

234 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition,!

Chapter 5: CPU Scheduling!

Page 2: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 2!

Chapter 5: CPU Scheduling!

•  Basic Concepts!•  Scheduling Criteria !•  Scheduling Algorithms!•  Thread Scheduling!•  Multiple-Processor Scheduling!•  Linux Example!

Page 3: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 3!

Objectives!

•  To introduce CPU scheduling, which is the basis for multiprogrammed operating systems!

•  To describe various CPU-scheduling algorithms!•  To discuss evaluation criteria for selecting a CPU-

scheduling algorithm for a particular system!

Page 4: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 4!

Basic Concepts!

•  The objective of multiprogramming is to have some process running at all time, to maximum CPU utilization.!

•  CPU–I/O Burst Cycle:!u Process execution consists of a cycle of ! CPU execution and I/O wait!u Process execution begins with a CPU burst that is

followed by an I/O burst, which is followed by another CPU burst , then another I/O burst , and so on,.. The final CPU burst ends the process.!

Page 5: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 5!

Alternating Sequence of CPU And I/O Bursts!

Page 6: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 6!

Basic Concepts!

CPU burst distribution!❖  A large number of short CPU bursts and a small number of long CPU bursts.!❖ An I/O –bound program has many short CPU bursts.!❖ A CPU –bound program has few long CPU bursts.!

! !

Page 7: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 7!

Histogram of CPU-burst Times!

A large number of short CPU bursts !

small number of long CPU bursts!

Page 8: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 8!

CPU Scheduler!

•  When the CPU becomes idle, the OS must select from among the processes in memory that are ready to execute, and allocates the CPU to one of them.!

•  The selection process is carried out by the short-term scheduler (CPU scheduler ).!

Page 9: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 9!

CPU Scheduler!

•  CPU scheduling decisions may take place when a process:!1. !Switches from running state to the waiting state(result of I/o request or wait for the termination of one of the child processes).!2. !Switches from running state to ready state(interrupt).!3. !Switches from waiting state to ready state(completion of I/O)!4. !Terminates!

•  Scheduling under only 1 and 4 is nonpreemptive or cooperative.!

•  All other scheduling is preemptive!

Page 10: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 10!

Preemptive scheduling!

•  Under nonpreemptive scheduling, once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state.!

•  Windows 95 and all subsequent versions of windows OS have used preemptive scheduling.!

Page 11: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 11!

Dispatcher!

•  The Dispatcher is the module that gives control of the CPU to the process selected by the short-term scheduler; this involves:!•  switching context!•  switching to user mode!•  jumping to the proper location in the user

program to restart that program!•  It should be fast.!•  Dispatch latency – the time it takes for the

dispatcher to stop one process and start another running!

Page 12: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 12!

Scheduling Criteria!

•  CPU utilization – keep the CPU as busy as possible.!•  Throughput – # of processes that complete their

execution per time unit(10 processes/second)!•  Turnaround time – amount of time to execute a

particular process(the interval from the time of submission of a process to the time of completion, waiting to get into memory, waiting in the ready queue, executing on the CPU, doing I/O). !

•  Waiting time – the amount of times a process has been waiting in the ready queue!

•  Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment)!

Page 13: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 13!

Scheduling Algorithm Optimization Criteria!

•  Max CPU utilization!•  Max throughput!•  Min turnaround time !•  Min waiting time !•  Min response time!

Page 14: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 14!

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.!!! !!

Page 15: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 15!

example!

! !Process! Burst Time!!! ! P1 !24!! ! P2 !3!! ! P3 ! 3 !• Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:!

• Waiting time for P1 = 0; P2 = 24; P3 = 27!• Average waiting time: (0 + 24 + 27)/3 = 17!

P1! P2! P3!

24! 27! 30!0!

Page 16: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 16!

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 as short processes go behind long process àlower CPU and device utilization.!

P1!P3!P2!

6!3! 30!0!

Page 17: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition!

Convoy effect: example!

P2!P3! P1!P4!P5!

P3!P4!P5!

Ready Queue!

Assume we have one CPU-bound process, P1, and many I/O-bound processes. P2-P5!

The CPU-bound process P1 will get and hold the CPU. !

CPU!

I/O device !

Running P1!

Running!P2!

I/ O Queue!

Page 18: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition!

Convoy effect: example!

P2!P3!P4!P5!

Ready Queue!

During this time, all the other processes will finish their I/0 and will move into the ready queue, waiting for the CPU !

CPU!

I/O device !

Running P1!

Idle !I/ O Queue!

Page 19: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition!

Convoy effect: example!

P3!P4!P5!

Ready Queue!

Eventually, the CPU-bound process finishes its CPU burst and moves to an I/0 device. !

CPU!

I/O device !

Running P2!

Running P1!

I/ O Queue!

Page 20: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition!

Convoy effect: example!

P2!P3!P4!P5!

Ready Queue!

All the I/O-bound processes, which have short CPU bursts, execute quickly and move back to the I/0 queues. !

CPU!

I/O device !

Idle!

Running!P1!

I/ O Queue!

At this point, the CPU sits idle. !

Page 21: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition!

Convoy effect: example!

P3!P4!P5!

Ready Queue! CPU!

I/O device !

Running P1!

Running!P2!

I/ O Queue!

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. !

Page 22: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 22!

Shortest-Job-First (SJF) Scheduling!

•  This algorithm associates with each process the length of its next CPU burst. !

•  Use these lengths to schedule the process with the shortest time, if the next CPU bursts of two processes are the same, FCFS scheduling is used.!

•  Two schemes: !•  Nonpreemptive – once CPU given to the process it

cannot be preempted until completes its CPU burst!•  Preemptive – if a new process arrives with CPU burst

length less than remaining time of current executing process, preempt. This scheme is known as the Shortest-Remaining-Time-First (SRTF)!

Page 23: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 23!

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!

!• Average waiting time = (3 + 16 + 9 + 0) / 4 = 7!• Compare with FCFS AWT=(0+6+14+21)/4=10.25!

P4! P3!P1!

3! 16!0! 9!

P2!

24!

Page 24: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 24!

Shortest-Job-First (SJF) Scheduling!

Example2: !Process !Arrival Time Burst Time !!! ! !P1 !0 ! !7!! ! !P2 !2 ! !4!! ! !P3 !4 ! !1!! ! !P4 !5 ! !4 !

• Non preemptive SJF!

P1! P3! P2!

7!P1(7)!

16!0!

P4!

8! 12!

Average waiting time = (0 + 6 + 3 + 7)/4 = 4!

2! 4! 5!

P2(4)!

P3(1)!

P4(4)!

P1‘s wating time = 0!P2‘s wating time = 6!P3‘s wating time = 3!P4‘s wating time = 7!

Page 25: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 25!

Shortest-Job-First (SJF) Scheduling!

Example3:!! ! !Process !Arrival Time Burst Time !!! ! ! P1 ! 0 ! !7!! ! ! P2 ! 2 ! !4!! ! ! P3 ! 4 ! !1!!! ! P4 ! 5 ! 4 !• Preemptive SJF(SRTF)!

P1! P3!P2!

4!2! 11!0!

P4!

5! 7!

P2! P1!

16!

Average waiting time = (9 + 1 + 0 +2)/4 = 3!

P1(7)!P2(4)!

P3(1)!

P4(4)!

P1‘s wating time = 9!P2‘s wating time = 1!P3‘s wating time = 0!P4‘s wating time = 2!

P1(5)!P2(2)!

Page 26: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 26!

Shortest-Job-First (SJF) Scheduling!

•  SJF is optimal – gives minimum average waiting time for a given set of processes!

•  The difficulty is knowing the length of the next CPU request.!

Page 27: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 27!

Prediction of the Length of the Next CPU Burst!

Page 28: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 28!

Priority Scheduling!

•  A priority number (integer) is associated with each process!•  The CPU is allocated to the process with the highest

priority (smallest integer ≡ highest priority in Unix but lowest in Java)!

•  Equal-priority processes are scheduled in FCFS order.!•  Preemptive: preempt the CPU if the priority of the

newly arrived process is higher than the priority of the currently running process. !

•  Nonpreemptive: put the new process at the head of the ready queue.!

Page 29: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 29!

Priority Scheduling!

•  SJF is a priority scheduling where priority is the predicted next CPU burst time!

•  Problem ≡ Starvation – low priority processes may never execute!

•  Solution ≡ Aging – as time progresses increase the priority of the process (for example : 1 every 15 minutes)!

Page 30: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 30!

Priority Scheduling!

Example : Process !Burst Time priority !!! ! !P1 ! 10 ! !3!! ! !P2 ! 1 ! !1!! ! !P3 ! 2 ! !4!! ! !P4 ! 1 ! !5!

p5 5 2 ! !!!!!!!The AWT is (6 +0+ 16+18+1)/5 = 8.2!

All arrived at time 0.!The Gantt chart for the schedule is:!

P2! P1!P5!

1! 16!0! 6!

P3!

19!18!

P4!

Page 31: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 31!

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!! 0 1 11 16 18 19!• Gantt chart: Preemptive priority scheduling!!! 0 1 3 8 16 18 19!

P2! P1! P5! P3! P4!

P2! P1! P5! P1! P3! P4!

Page 32: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 32!

Round Robin (RR)!

•  Is designed especially for time-sharing systems.!•  Similar to FCFS, but it is Preemptive to enable

the system to switch between processes.!•  Each process gets a small unit of CPU time (time

quantum or time slice), usually 10-100 milliseconds. !

•  The Ready queue is FIFO (new processes are added to the tail of the queue.)!

•  The CPU scheduler picks the first process from the ready queue, set a timer to interrupt after 1 time quantum, and dispatch the process.!

Page 33: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 33!

Round Robin (RR)!•  One of two things will happen!

•  The process may have a CPU burst of < 1 time quantum à the process itself will release the CPU voluntarily.!

•  The CPU burst of the currently running process > 1 time quantum ! àthe timer will go off and will cause an interrupt to the OS à a context switch will be executed, and the process will be put at the tail of the ready queue.!

•  The CPU scheduler will then select the next process in the ready queue.!

•  Typically, higher average turnaround than SJF, but better response!

Page 34: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 34!

Round Robin (RR)!Example1:!Time quantum = 4!!! !Process !Burst Time!! !P1 !24!! ! P2 ! 3!! ! P3 !3!• The Gantt chart is:!

!!• AWT(6(10-4)+4+7)/3 = 5.66!

P1! P2! P3! P1! P1! P1! P1! P1!

0! 4! 7! 10! 14! 18! 22! 26! 30!

Page 35: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 35!

Round Robin (RR)!• 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!

Average wait time = (81+20+94+97)/4 = 73!

57!20!

37!

57!

24!

40!

40!17!

P1(53)!

P2(17)!P3(68)!P4(24)!

P1(33)! P1(13)!

P3(48)! P3(28)! P3(8)!

P4(4)!

Page 36: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 36!

Round Robin (RR)!•  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 until its next time quantum. !

•  (Ex: 5 processes, TQ = 20 milliseconds, each process will get up to 20 milliseconds every 100 milliseconds.!

•  The Performance of RR depends heavily on the size of the TQ.!•  TQ large ⇒ FCFS!•  TQ small ⇒ TQ must be large (but not too large) with

respect to context switch time, otherwise overhead is too high!

Page 37: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 37!

Time Quantum and Context Switch Time!

Page 38: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 38!

Turnaround Time Varies With The Time Quantum!

The average TurnAroundTime of a set of process does not necessarily improve as the TQ size increase. The AVG TAT can be improved if most process finish their next CPU burst in a single time quantum.!

Page 39: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 39!

Multilevel Queue!

•  Processes are classified into different groups.!•  Each group have different response-time requirements

àdifferent scheduling needs.!!A multilevel queue scheduling algorithm partitions the Ready queue into separate queues:!

•  foreground (interactive)!•  background (batch)!

Page 40: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 40!

Multilevel Queue!

•  Each queue has its own scheduling algorithm!•  foreground – RR!•  background – FCFS!

•  Scheduling must be done between the queues!•  Fixed priority preemptive scheduling; (i.e., serve all from

foreground then from background). Possibility of starvation.!

•  Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR, 20% to background in FCFS !

Page 41: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 41!

Multilevel Queue Scheduling!

Page 42: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 42!

Multilevel Feedback Queue!

•  Implement multiple ready queues!•  Allows process to move between queues.!•  Just like multilevel queue scheduling, but

assignments are not static!•  Different queues may be scheduled using

different algorithms!

•  Multilevel feedback queue-scheduling algorithm allows a process to move between the various queues; aging can be implemented this way!

Page 43: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 43!

Multilevel Feedback Queue!

•  Multilevel-feedback-queue scheduler defined by the following parameters:!•  number of queues!•  scheduling algorithms for each queue!•  method used to determine when to upgrade and

downgrade a process!•  The most general CPU-scheduling algorithm. !•  The most complex algorithm.!

Page 44: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 44!

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.!

Page 45: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 45!

Multilevel Feedback Queues!

Page 46: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 46!

Thread Scheduling!

•  Distinction between user-level and kernel-level threads!•  On OSs that support them, it is the kernel-level threads-not

processes- that are being scheduled by OS.!•  User-level threads are managed by a thread library and the

kernel is unaware of them.!•  To run on CPU, the user level threads must be mapped to

an associated kernel-level thread. It may use a lightweight process(LWP).!

contention scope:!•  one distinction between user-level and kernel-level

threads lies in how they are scheduled.!

Page 47: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 47!

Thread Scheduling!

•  Many-to-one and many-to-many models, thread library schedules user-level threads to run on LWP. Known as process-contention scope (PCS) since scheduling competition takes place among threads belonging to the same process.!

•  PCS is done according to preempt priority.!•  PTHREAD SCOPE PROCESS schedules threads using PCS

scheduling.!•  Kernel thread scheduled onto available CPU is system-contention

scope (SCS) – competition takes place among all threads in system!•  Systems using the one-to-one model schedule threads using only

SCS.!•  PTHREAD SCOPE SYSTEM schedules threads using SCS

scheduling.!

Page 48: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 48!

Multiple-Processor Scheduling!

•  CPU scheduling more complex when multiple CPUs are available!•  Different rules for homogeneous processors (Identical

processors in terms of their functionality) or heterogeneous processors.!

1. Asymmetric multiprocessing:!•  All scheduling decisions, I/O processing, and other system

activities handled by a single processor – the master server.!•  The other processors execute only user code.!•  Simple because only one processor accesses the system data

structures, reducing the need for data sharing.!

Page 49: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 49!

Multiple-Processor Scheduling!

•  CPU scheduling more complex when multiple CPUs are available!•  Different rules for homogeneous processors (Identical

processors in terms of their functionality) or heterogeneous processors.!

1. Asymmetric multiprocessing:!2. Symmetric multiprocessing (SMP):!•  each processor is self-scheduling, all processes in common ready

queue, or each has its own private queue of ready processes!•  Multiple processors try to access and update a common data

structures. So, scheduler must be programmed carefully.!•  Must ensure that 2 processors don’t choose the same process.!

Page 50: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 50!

Linux Scheduling!

•  Linux Scheduler is a preemptive, priority-based algorithm with 2 separate priority ranges:!•  Two priority ranges: time-sharing and real-time!•  A real-time range from 0 to 99 à Longer time quantum!•  A nice value ranging from 100 to 140 à Shorter time quantum!

Page 51: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 51!

Linux Scheduling!

•  The kernel maintains a list of all runnable tasks in a runqueue data structure.!

•  Each runqueue contains two priority arrays : !•  Active:! contains all tasks with time remaining in their time slices !•  Expired:! contains all expired tasks.! !

Page 52: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 52!

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).!

Page 53: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 53!

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!

Page 54: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 54!

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)!

Page 55: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 55!

Simulations!

Page 56: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition! 56!

Conclusion!

•  We’ve looked at a number of different scheduling algorithms.!

•  Which one works the best is application dependent.!

•  General purpose OS will use priority based, round robin, preemptive!

•  Real Time OS will use priority, no preemption.!

Page 57: Chapter 5: CPU Scheduling - Operating System · Chapter 5: CPU Scheduling! ... Shortest-Remaining-Time-First (SRTF)! Operating System Concepts – 8th Edition! 23! Silberschatz, Galvin

Silberschatz, Galvin and Gagne ©2009!Operating System Concepts – 8th Edition,!

End of Chapter 5!