BATCH SCHEDULING ALGORITHMS Module 2.5 COP4600 – Operating Systems Richard Newman SCHEDULING IN BATCH SYSTEMS • Algorithms • First-Come First-Served • Shortest Job First • Shortest Remaining Time Next • Non-preemptive Priority • Preemptive Priority • Metrics • Mean Turnaround Time • Processor Utilization Tanenbaum & Bos, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved. FIRST-COME FIRST-SERVE (FCFS) A A arrives at time 0, take 9 units B arrives at time 1, takes 4 units B C C arrives at time 2, takes 3 units What is FCFS Schedule? B arrives at time 1 C arrives at time 2 C must wait B must wait A finishes at time 9 B finishes at time 13 A running Now B can run Now C can run B waiting B running C finishes at time 16 C waiting C runs All done! Schedule A arrives at time 0 A gets CPU A B C MTT= (9+12+14)/3= 35/3 TT(A) = 9 – 0 = 9 TT(B) = 13 – 1 = 12 TT(C) = 16 – 2 = 14 SHORTEST JOB FIRST (SJF) A arrives at time 0, take 9 units A B arrives at time 1, takes 4 units B C arrives at time 2, takes 3 units C What is SJF Schedule? Schedule A arrives at time 0 A gets CPU C arrives at time 2 C must wait B arrives at time 1 B must wait A A A B B C C goes next – why? C has shorter run time A finishes at time 9 Who gets CPU next? A running B waiting wait… C waiting C runs TT(A) = 9 – 0 = 9 C finishes at time 12 Now B runs.. And finishes at time 16 B running TT(C) = 12 – 2 = 10 MTT= (9+10+15)/3= 34/3 TT(B) = 16 – 1 = 15 SJF IS OPTIMAL… IF ALL JOBS READY Assume all jobs arrive at time 0 (i.e., all jobs are ready) Let jobs be numbered in order of time required C1 < C2 < … < CN SJF schedule is J1, J2, … , JN Suppose “optimal” schedule S is not SJF At some point, there must be long job before shorter job S J1 J2 … SJF J1 J2 … … Ji+k Ji … Ji Ji+k … … Swap job Ji+k with Ji in “optimal” schedule S All jobs before or after swap finish at same time as before Ji+k finishes at same time as Ji finished in S Ji finishes at earlier time in SJF than Ji+k finished in S So do all jobs between Ji+k and Ji in S! SJF has smaller MTT – contradiction! OPTIMAL NON-PREEMPTIVE A arrives at time 0, take 9 units A B arrives at time 1, takes 4 units B C arrives at time 2, takes 3 units C What is Optimal Schedule? (using MTT) Schedule A arrives at time 0 A waits! – Why? C arrives at time 2 C must wait – Why? B arrives at time 1 B runs A B C A A A waits A waits B finishes at time 5 Who gets CPU next? A running B B runs C finishes at time 8 Now A finally runs… and finishes at time 17 Sure beats 34/3! C waits C runs TT(B) = 5 – 1 = 4 C goes next – why? C has shorter run time TT(C) = 8 – 2 = 6 MTT= (4+6+17)/3= 27/3 TT(A) = 17 – 0 = 17 Tanenbaum & Bos, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved. SHORTEST REMAINING TIME FIRST (SRTF) A arrives at time 0, take 9 units A B arrives at time 1, takes 4 units B C arrives at time 2, takes 3 units C SRTF is Optimal Preemptive Schedule (using MTT) Schedule A arrives at time 0 A runs C arrives at time 2 C should wait – Why? B arrives at time 1 B runs – why? A B C A A A waits A waits C goes next – why? C has shorter run time B finishes at time 5 Who gets CPU next? A running C finishes at time 8 Now A finally runs again and finishes at time 16 B B runs C waits C runs TT(B) = 5 – 1 = 4 TT(C) = 8 – 2 = 6 MTT= (4+6+16)/3= 26/3 TT(A) = 16 – 0 = 16 Tanenbaum & Bos, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved. SRTF IS OPTIMAL Similar argument to SJF optimality But no need to constrain arrival times! Main assumption: context switching time is zero Not necessarily a good assumption! Example: Suppose context switching takes 1 time unit A arrives at time 0, take 10 units A B arrives at time 2, takes 4 units B A A A A waits 6 B B B runs 4 OS S S A runs 9 S TT(B) = 7 – 2 = 5 TT(A) = 17 – 0 = 17 MTT = 17 + 5 = 22/2 = 11 Exercises: First – what is FCFS MTT for example? Second: Come up with job set for which SRFT is NOT optimal! FCFS, SJF, SRTF SCHEDULING Process Name: Arrival Time (AT): CPU Burst Length (CT): A 0 8 B 2 5 C 3 1 D 5 2 E 7 2 FCFS Schedule: A A A A A A A A B B B B B C D D E E Turnaround times: A:8-0, B:13-2, C:14-3, D:16-5, E: 18-7 Average TT = (8+11+11+11+11)/5 = 52/5 = 10.4 SJF Schedule: A A A A A A A A C D D E E B B B B B Turnaround times: A:8-0, B:18-2, C:9-3, D:11-5, E: 13-7 Average TT = (8+16+6+6+6)/5 = 42/5 = 8.4 SRTF Schedule: A A B C B D D E E B B B A A A A A A Turnaround times: A:18-0, B:12-2, C:4-3, D:7-5, E: 9-7 Average TT = (18+10+1+2+2)/5 = 33/5 = 6.6 SJF SCHEDULING Figure 2-41. An example of shortest job first scheduling. (a) Running four jobs in the original order (FCFS). (b) Running them in shortest job (SJF) first order. What is the mean turn-around time for the set of jobs? FCFS A:8, B:12, C:16, D:20 Mean = 56/4 = 14 SJF A:20, B:4, C:8, D:12 Mean = 44/4 = 11 Tanenbaum & Bos, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved. PRIORITY SCHEDULING • Processes assigned a priority • May be given externally • May be computed internally • Non-preemptive • Current process runs until burst done • Preemptive • Newly ready process can preempt current process, which resumes later • Generality • Priority function can be used to model other algorithms SUMMARY • Metrics • Mean Turnaround Time • • • • FCFS SJF SRTF Priority
© Copyright 2026 Paperzz