15 - Scheduling (2)OutlineBatch Systems SchedulingFirst Come, First Serve (FCFS)AnnouncementsReading:Batch Systems Scheduling (FCFS, SJF)Interactive Systems Scheduling (Round Robin)Round Robin vs. FCFSAssumptions:Algorithms:A First-In, First-Out (FIFO) queue.Example:Turnaround Time =Turnaround Time:Average TT = 13P1: TT = (30 - 0) = 30P3: TT = (6 - 0) = 6P2: TT = (3 - 0) = 3(completion time - release time)or(wait time + running time)Average Turnaround Time = e.g. even though P3 is short, its turnaround time isvery long since P1 arrived first.Convoy Effect: Long-running processes delay turnaroundtime for shorter processes.When we change the arrival order: P2, P3, P1Takeaway: The convoy effect makes FCFS a poor choice.Assumption: Each job's processing time is already knownSchedule the next job with the shortest processing timeProcesses become availablein order: P1, P2, P3Goals:Will release PA 3 instructions by tomorrow.Quiz 2 this Friday1)2)3)MOS 2.4--------each job's processing time is known (for SJF)jobs are assigned to CPU in order of release timenon-preemptive: jobs are run-to-completionThroughputFirst Come, First Serve (FCFS)Shortest Job First (SJF)batch system tasks are often repetitive, so processing time canbe measured and/or approximatede.g. time to process monthly payroll would be approximately sameP1P1: TT = 24 - 0 = 240243300ReleaseTimeProcessingTimeP2P2: TT = 27 - 0 = 27P3P3: TT = 30 - 0 = 30P2P3P1t = 0ready:P1P2P3t = 24ready:P2P3t = 27ready:P3t = 30ready:t = 0ready:P2P3P1t = 3ready:P3P1t = 6ready:P1t = 30ready:Shortest Job First (SJF)--Non-preemptive:Example:Non-preemptive:Preemptive:SRTN minimizes average turnaround time.SRTN Disadvantages:Starvation:would need to modify SJF to prevent starvationA job never completes due to beingcontinuously preempted- jobs are run-to-completion- Shortest Remaining Time Next (SRTN)- kernel switches jobs if another arrives with shorter remaining processing timePreemptive:only valid when performing same tasks (measured time)otherwise you can't avoid convoy effectP107414245ReleaseTimeProcessingTimeP2P3P4t = 0ready:P1 (p=7)t = 7ready:P2 (p=4)P3 (p=1)P4 (p=4)t = 8ready:P2 (p=4)P4 (p=4)t = 12ready:P4 (p=4)t = 16ready:P4 (p=4)P1P1P2P2P3P3P4P4t = 0ready:P1 (p=7)t = 2ready:P1 (p=5)P2 (p=4)t = 4ready:P1 (p=5)P2 (p=2)P3 (p=1)t = 5ready:P1 (p=5)P2 (p=2)P4 (p=4)t = 7ready:P1 (p=5)P4 (p=4)t = 11ready:P1 (p=5)t = 16ready:P1P1P2P2P3P3P4P4---requires known processing times (unrealistic)more context switching (switching overhead)potentially starves jobsTurnaround Time:Average TT = 8Turnaround Time:P1: TT = 7P1: TT =P3: TT = 12 - 4 = 8P3: TT =P4: TT = 16 - 5 = 11P4: TT =P2: TT = 8 -2 = 6P2: TT =Interactive Systems SchedulingAlgorithms:Each job runs for specified time quantumAfter time quantum elapsed:If job terminates or blocks:An old, popular algorithm.Note: release time onlydetermines position in queueAssume ascending order.Assume time quantum = 20Example:Goals:--------------Response Time(not necessarily completion)ProportionalityRound RobinPriority SchedulingMultiple Queuestime quantum = fixed time intervalrunning job suspended (preempted)job loses rest of quantumsimple: it's a FIFO queuefair: every job is guaranteed processing timenext job in ready queue immediately runsjob added to the end of the ready queuenext job in ready queue runsrequires preemption via clock interruptAverage Turnaround TimeCPU UtilizationRound Robin (RR)P15286624ProcessingTimeP2P3P4t = 0ready:P1:52P2:8P3:66P4:24t = 20ready:P2:8P3:66P4:24P1:32t = 28ready:P3:66P4:24P1:32t = 48ready:P4:24P1:32P3:46t = 68ready:P1:32P3:46P4:4t = 88ready:P3:46P4:4P1:12t = 108ready:P4:4P1:12P3:26t = 112ready:P1:12P3:26t = 124ready:P3:26t = 150ready:P1P2P3P4Waiting Time:Selection depends on:Average = Average = Turnaround Time:P1: WT =P1: TT =P3: WT =P3: TT =P4: WT =P4: TT =P2: WT =P2: TT =Selecting the Time QuantumExercisePollEv---context switching time (quantum >> switching)Longer quantum => increase response timeShorter quantum => higher switching overheadNote: nowadays, time quantum is not fixedGiven:When will the 50th job start?switching time = 1ms, quantum = 4ms => 20% time switchingmore jobs => increases response time for jobs at end of queuewant switching to be < 1% of total CPU timeusually switching order ~us, quantum order ~ms, 1000x longer=> requires interrupt frequency ~100Hz - 1000Hznumber of jobskernel can decide a quantum on per-job basisquantum = 100msswitching time = 1ms50 jobs in queue with p > quantumAdvantages:Disadvantages:--all jobs guaranteed CPU time=> no starvationaccumulates switching timeincreases average waiting timedifferent processing times=> low average waiting timenot too many jobs in queue=> good response time-----FairnessMany long jobsFew, diverse jobsFCFS vs. RRConsider the following scenario:Round Robin is bad when the job lengths are longand the same.FCFS has better cache efficiency and switching.When quantum > max processing time, RR = FCFSFCFS:Average TT = 550msNum switches = 9Num switches = 999Average TT = 995.5msRR:10 jobs, 100ms processing time eachRR time quantum = 1msAll jobs are released simultaneouslyRR causes cache misses, increasing switch timeP1P1100991200992......10001000CompletionTimeCompletionTimeP2P2......P10P10P15386824ProcessingTimeP2P3P4Revisit RR Example:WTScenarioBest FCFSBest FCFS3285088515383257815882568068928811212114531.2569.562100.561.2599.561.2599.566.25104.583.5121.7557.2595.585153851538515385153851530682230202881610182028145153841378213580133821357213768121Q = 1Q = 1Q = 5Q = 5Q = 8Q = 8Q = 10Q = 10Q = 20Q = 20Worst FCFSWorst FCFSP1P2P3P4AverageTTBest-case FCFS (no convoy):Worst-case FCFS is reverse.P28P424P153P368t = 8t = 32t = 85t = 153t = 0P1P2P2P2 Waiting= (24 + 27 + 30) / 3 = 2716516Average TT = 7(68-20)+(112-88) = 7212428150112103.5(20 - 0) = 20(28)+(88-48)+(124-108) = 84(48)+(108-68) = 88664949