cis 415 cpu scheduling scheduling terminology -...
TRANSCRIPT
CIS 415
Introduction to Scheduling 1
1
CPU Scheduling
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms– FCFS, SJF, RR
• Exponential Averaging
• Multi-level Queue Scheduling
• Performance Evaluation
2
Scheduling TerminologyLong term scheduling (job scheduling) -decision as to which jobs to select forthe pool of active processes. Medium term scheduling - decision tomove some of the processes out ofmain memory.Short term scheduling (processscheduling) - decision as to whichprocess to allocate to the CPU.
3
Scheduling TerminologyContext switch (dispatch) - the actions takento make the selected process run on the CPU- restore CPU state to that of the selectedprocess when it last ran (all stored in the PCB- registers, cc, PC)Degree of multiprogramming - the number ofactive processes (I.e. in the ready andrunning states.)Swapping - the actions taken to remove aprocess from memory to reduce the degreeof multiprogramming.
4
Scheduling/Process State Transition
5
Why/When Processes SwitchKey Data Structures_ Process Control Block (PCB)_ Ready Queue_ Blocked Queue
6
Objectives for SchedulersGOOD PERFORMANCE FOR THE USER (basedon some metric)_ high throughput - number of jobscompleted per time unit_ low turnaround time - elapsed time fromsubmission to completion, averaged over aset of jobs._ low weighted avg. turnaround time - sameas above but certain jobs are given moreweight._ low response time - elapsed time fromsubmission to some response(timesharing).
CIS 415
Introduction to Scheduling 2
7
Objectives for Schedulers (cont’d)GOOD PERFORMANCE FOR THE SYSTEM(based on some metric)_ CPU Utilization - percent of time the CPU isengaged in useful work over some timeframe.
FAIRNESS_No process should get postponedindefinitely (priorities)
STABILITY_ predictability_ graceful degradation
8
CPU burst and I/O Burst• Maximum CPU utilization obtained with
multiprogramming
• CPU–I/O Burst Cycle – Process executionconsists of a cycle of CPU execution and I/Owait.
• CPU burst distribution
9
Alternating Sequence of CPU AndI/O Bursts
10
Histogram of CPU-burst Times
11
Types of SchedulersPREEMPTIVE versus NON-PREEMPTIVE_ Preemptive - can interrupt a process duringits CPU burst and schedule a differentprocess._ Non-preemptive - each process completes itsCPU burst before next process is scheduled.(Can't be used for timesharing.)
PRIORITIES (STATIC vs. DYNAMIC)_ Static - fixed for the life of the process;dynamic - changes over the life of theprocess._ Determined by characteristics of theprocess._ Aging - dynamically changing the priorityover time (to give jobs that have waiting along time higher priority).
12
CPU (Short Term) Scheduler• Selects from among the processes in memory
that are ready to execute, and allocates theCPU to one of them.
• CPU scheduling decides which process toswitch from ready state to running state.
CIS 415
Introduction to Scheduling 3
13
CPU Scheduling Algorithms• First Come First Served (FCFS)
• Shortest Job First (SJF)
• Round Robin (RR)
• Multi-level Queues
• Multi-level with Feedback
• (There are many others)
14
First-Come, First-Served (FCFS)Scheduling
Process Burst TimeP1 24
P2 3 P3 3
• Suppose that the processes arrive in the order:P1 , P2 , P3The 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 300
15
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
P1P3P2
63 300
16
Shortest-Job-First (SJF)Scheduling
• Associate with each process the length of its nextCPU burst. Use these lengths to schedule theprocess with the shortest time.
• 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 executingprocess, preempt. This scheme is know as theShortest-Remaining-Time-First (SRTF).
• SJF is optimal – gives minimum average waitingtime for a given set of processes.
17
Process Arrival TimeBurst TimeP1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4
• SJF (non-preemptive)
• Average waiting time = (0 + 6 + 3 + 7)/4 - 4
Example of Non-Preemptive SJF
P1 P3 P2
73 160
P4
8 12
18
Example of Preemptive SJFProcess Arrival TimeBurst Time
P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4
• SJF (preemptive)
• Average waiting time = (9 + 1 + 0 +2)/4 - 3
P1 P3P2
42 110
P4
5 7
P2 P1
16
CIS 415
Introduction to Scheduling 4
19
Priority Scheduling• A priority number (integer) is associated with each
process• The CPU is allocated to the process with the
highest priority (smallest integer ≡ highestpriority).
• SJF is a priority scheduling where priority is thepredicted next CPU burst time.
• Problem ≡ Starvation – low priority processes maynever execute.
• Solution ≡ Aging – as time progresses increase thepriority of the process.
20
Determining Length of NextCPU Burst for SJF
• Can only estimate the length.
• Can be done by using the length of previousCPU bursts, using exponential averaging.
:Define 4.
10 , 3.
burst CPU next the for value predicted 2.
burst CPU of lenght actual 1.
££
=
=
+
aa
t 1n
thn nt
( ) .t nnn taat -+== 11
21
Prediction of the Length of the NextCPU Burst
22
Examples of ExponentialAveraging
• a =0– tn+1 = tn
– Recent history does not count.• a =1
– tn+1 = tn
– Only the actual last CPU burst counts.
• If we expand the formula, we get:tn+1 = a tn+(1 - a) a tn -1 + … +(1 - a )j a tn -1 + … +(1 - a )n=1 tn t0
• Since both a and (1 - a) are less than or equal to 1,each successive term has less weight than itspredecessor.
23
Exponential Averaging
24
Round Robin (RR)• Each process gets a small unit of CPU time (time
quantum), usually 10-100 milliseconds. After thistime has elapsed, the process is preempted andadded to the end of the ready queue.
• If there are n processes in the ready queue and thetime quantum is q, then each process gets 1/n of theCPU time in chunks of at most q time units at once.No process waits more than (n-1)q time units.
• Performance– q large fi FIFO– q small fi q must be large with respect to context
switch, otherwise overhead is too high.
CIS 415
Introduction to Scheduling 5
25
Example of RR with TimeQuantum = 20
Process Burst TimeP1 53 P2 17 P3 68 P4 24
• The Gantt chart is:
• Typically, higher average turnaround thanSJF, but better response.
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
26
Time Quantum and ContextSwitch Time
27
Turnaround Time Varies With TheTime Quantum
28
Multilevel Queue• Ready queue is partitioned into separate queues:
foreground (interactive)background (batch)
• Each queue has its own scheduling algorithm,foreground – RRbackground – FCFS
• Scheduling must be done between the queues.– Fixed priority scheduling; (i.e., serve all from
foreground then from background). Possibility ofstarvation.
– Time slice – each queue gets a certain amount of CPUtime which it can schedule amongst its processes; i.e.,80% to foreground in RR
– 20% to background in FCFS 29
Multilevel Queue Scheduling
30
Multilevel Feedback Queue• A process can move between the various queues;
aging can be implemented this way.
• Multilevel-feedback-queue scheduler defined bythe following parameters:– number of queues– scheduling algorithms for each queue
– method used to determine when to upgrade a process
– method used to determine when to demote a process– method used to determine which queue a process will
enter when that process needs service
CIS 415
Introduction to Scheduling 6
31
Example of Multilevel FeedbackQueue
• Three queues:– Q0 – time quantum 8 milliseconds– Q1 – 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 notfinish in 8 milliseconds, job is moved to queue Q1.
– At Q1 job is again served FCFS and receives 16additional milliseconds. If it still does not complete, itis preempted and moved to queue Q2.
32
Multilevel Feedback Queues
33
Performance for SchedulersQueueing Theory Analysis - uses well-established mathematical models andtechniques.Simulation - create a model of the systemand simulate its performance usingsimulation software.Empirical Experiments - implement andtest the algorithms in a real system.
34
Queuing Theory AnalysisSingle-server Queue:
35
Queuing Theory Analysis (cont’d)
36
Queuing Theory Analysis (cont’d)Multiple-server Queue:
CIS 415
Introduction to Scheduling 7
37
Queuing Theory Analysis (cont’d)
Inputs:_arrival rate - from a probabilitydistribution (usually Poisson whichimplies random arrivals)_service time - from a probabilitydistribution (often exponential)_scheduling discipline/algorithm
38
Queuing Theory Analysis (cont’d)
Outputs:_ Items waiting_Waiting time_ Items queued_Queuing time
39
Simulation Analysis
Discrete-event Simulation_Often uses models similar to queueinganalysis_More detailed or more realisticparameters (e.g. trace driven)_Simulates events step by step andgathers statistics rather than usingmathematical formulas
40
Evaluation of CPU Schedulers bySimulation
41
Simulation Analysis (cont’d)
42
Empirical Experiments
Properties:_Costly and time-consuming_Sometimes not possible_More realistic
CIS 415
Introduction to Scheduling 8
43
Solaris 2 Scheduling
44
Windows 2000 Priorities