Uniprocessor Scheduling 1 0 Priorities • Scheduler will always choose a process of higher priority over one of lower priority • Have multiple ready queues to represent each level of priority • Lower-priority may suffer starvation – To overcome that allow a process to change its priority based on its age or execution history 2 3 Process Scheduling Example 4 1- First-Come-First-Served (FCFS) • Each process joins the Ready queue • When the current process finishes to execute, the oldest process in the Ready queue is selected • A short process may have to wait a very long time before it can execute • Favors CPU-bound processes – I/O processes have to wait until CPU-bound process completes 5 Simple Gantt Chart 6 Example (FIFO) Job # Jobtime # Arrival 1 2 3 4 5 85 Arrival time Finish time 01 30 22 92 43 134 64 186 208 Servicetime time Response 33 76 94 125 122 Service time 3 6 4 5 2 Wait time 0 1 5 7 10 Average response time = (3+7+9+12+12)/5 = 8.6 Throughput = 5/20 = 1/4 Ave (RespTime/ServTime) = (1+7/6+9/4+12/5+12/2)/5 = 2.564 2- Round-Robin • Uses preemption based on a clock • An amount of time is determined that allows each process to use the processor for that length of time • Clock interrupt is generated at periodic intervals • When an interrupt occurs, the currently running process is placed in the ready queue – Next ready job is selected • Known as time slicing 8 Example (Round Robin) Job # Arrival time Arrival time 1 00 2 22 3 44 4 66 55 88 Finish time Service time Response time 43 4 18 6 16 17 4 13 20 5 14 15 2 7 Service time 3 6 4 5 2 WaitJtime 14enters 10 9J4 9esxpires, 5 ssume J 2 J3 time Jenters, 2J2Jquantum time Jenters, J5 expires enters, before 3expires time 2 J5 time esJ1 time expires expires expires time expir eexpires 1A time quantum 2expires J1 time Jfinish Jthen expires Jthen J3 4 Jtime ti3me Jtime 2 J5 expires finishes expires Average response time = (4+16+13+14+7)/5 =J4, QueueQueueQueue: QueueJ1: QueueJ1: e2J5:uJ5,ueue: 5queue,J4, J5,eue ,J:10.8 J4J4JJ3, 4,3J:,3,JJ2 ueuSeJ1,Jwitch :1Qto eJ2to J:2Qwitch ueS ue,J3witch :3Q ,J2S :(JJ3 2Qwitch ueS uJ2 e,J2witch 4J:uS e4Q uS eJ3 ue,J3witch ,w a,Js,J:3J)JQ J3,J2witch :u2Switch ,JiQ Q5J5,to JQueueJ2: S,Qwitch Sueuwitch S S Sto witch J2 to 1 Scontinues to J1 J2 touewitch to to J4 witch to J2 23S ,J2, to J5 2to J4 ,J,J,Jn2J5J3 ,J4Queue: J3to J5 J4 J4, 1 Jstarts J3, J2 witch to J2 Round Robin T=1 Example (Round Robin) Job # Arrival time 1 0 2 2 3 4 44 66 55 88 Finish time Service time Response time 53 5 17 6 15 13 4 9 20 5 14 15 2 7 Service time 3 6 4 5 2 5 Wait time 24 Jenters 9J2 5 pir 9 finishes J2 enters, J3 Jenters, 1 Jthen finishes J5 time enters expires time 3 J1 then time J2 ti expires me J4 expires time J3 ex expires finishes Jes 5 finishes J2 J4 time exp ires Average response timeQueueJ2,QueueJ:1QueueJ1,: = (5+15+9+14+7)/5 10J4eue: QueueQueue: J1Queue: e53uJ,e5Jeue: :,5,JJ52J=2J2, ,eue: J4 J4 QueuseJ3tarts ,J:3Swi ,QueJ2J:2,J2tch ,J:uS e4Quwitch eto uSeJ4witch ,J:4JJ2 ,JJ43J3,Q J:uwitch J3J,JJ1 Qutotch Quto J1 Scontinues Sw Swi Swi Swi Swi J4 Continues to2Qu J3 continues to toitch J2 totch J4 to J2J4 J3 , J4tch J2 J4 tch to J5 Round Robin T=2 • Round-Robin – Process take turns – Each executes till its time-quantum expires – Gives relatively reasonable turnaround time • Better in standard deviation of turnaround time – Useful when it is hard to estimate the execution time • No need to know execution time in advance – Overhead on process switching • More process switch more context switch – I/O bound jobs may not use up their time quantum • Not fair to I/O bound jobs 12 3- Shortest Process Next • Nonpreemptive policy • Process with shortest expected processing time is selected next • Short process jumps ahead of longer processes • Predictability of longer processes is reduced • If estimated time for process not correct, the operating system may abort it • Possibility of starvation for longer processes 13 Example (Shortest Job Next) Job # Arrival Arrival time time 11 00 22 22 3 3 4 4 4 4 6 6 5 5 8 8 Finish time Service time Response time 33 3 69 7 4 15 11 5 20 14 2 11 3 Service time 3 6 4 5 2 511+14+ 2Job finishes Average respons e0time Job = 1(3+7+ Wait time 7finishes 3)/5 9 = 7.16 Job 2Job 5 enters Jobs in queu e 4Job enters Jobs 3 Job enters 3, 4, 53,in4 queue enters Throughput = 5/20 = 1/4 But has Buttohas Job 3, Job 52the 3allisshortest inthe queue shortest 3finish Job 54, isqueued tos Jobs finish ,Job 4 both 1Job Ave (RespTime/ServTime) = (1+7/6+11/4+14/5+3/2)/5 = 1.844 SJF 4- Shortest Remaining Time • Preemptive version of shortest process next policy • Must estimate processing time 15 Example (Shortest Remaining Time) Job # Arrival time 1 0 Finish time Service time Response time 33 3 2 2 3 4 4 6 55 88 15 6 48 Job13 5 enters 4 20 5 14 10 2 2 Job enters 3 4 Job 63 finishes4 5 2 Job 2 Job Remaining Job 5 finishes aining time of time Jobof2 Job = 52 = 5 enters 3Rem enters Wait t i m e 0 7 0 Average response time =of (3+13+4+14+2)/5 7.2 Remaining J ob Re1maining Re m Re Remaini ng aining maining 3 Job = 2of= 49 =Job 5 2 =05 finishes time time Jobtime of1 Job =of 1time 2Job = of 5time Throughput =Re 5/20 = aining 1/4 Remaining Job Re Remaining e of maining time of Job = 535fin =Job 2ishe4 2maining isRem time the only of time Job job of2 Job = of 5time 3Job =tim 4J4ob Ave (RespTime/ServTime) = Ch (1+13/6+1+14/5+1)/5 = 1.6 =s5ontinue Cwitch S to Swi tinue Job Job53 Choose Job tch with to Job Job Job 2Con 3oose 1with 2Switch to Job 4 Service time SRTF 5- Highest Response Ratio Next (HRRN) • Choose next process with the greatest ratio (time spent waiting + expected service time) expected service time •The process with the greatest value will be scheduled to run. 17 Example (Highest Response Ratio) RR = (waiting-time + service-time) / service-time Job # Arrival Arrival time time 11 00 22 22 33 44 4 4 6 6 5 5 8 8 Finish time Service time Response time 33 3 69 7 4 13 9 5 20 14 2 15 7 Job62 finishes4 5 2 RR of JobJob finishes = 2.25 3 =3((9–4)+4)/4 Average respons eJob time =50(3+7+ 9+14+7 8= 0–6)+5)/5 Wait time 0 4Jenters 94 ==((13 Job 1 finishes of JobRR Job)/5 = 2.4 47=of ((9–6)+5)/5 1.6 obRR enters Throughput =Job 5/20 = 1/4 JobJ2ob Jobs Job 5 = ((13–8)+2)/2 = 3.5 2 is 3Jobs enters the only job 5queue =of ((9–8)+2)/2 = 1.5 3, 4RR in 3,of queue 4,J5obinRR enters Ave (RespTime/ServTime) = 2.144 JobS1witch Has Has H Choose J ob Choose tofinsih finish Job 2finish Job 2(1+7/6+9/4+14/5+7/2)/5 3 2 Job 5 to as to=finish Job 2Job has to Service time HRRF 3 • 6- Multilevel Feedback Queue – The system maintains multiple ready queues • For example, N priority levels – CPU always serves the jobs in the highest priority queue – A new process will be assigned a high priority – If a process used up its time quantum • priority decremented by one (placed in a lower level queue) – Favor I/O bound jobs • Not use up time quantum stay at the same priority level – Good for time sharing systems • There are many variations of the algorithm 20 21 22 Example (Multi-level Queue) Job # Arrival time 11 00 22 22 33 44 44 66 55 88 Finish time Service time 311 620 4 16 5 19 210 Response 11 12 13exp 2 J1 Jenters JJ3 5e Jfinishes 1 Jtime time 2 Jenters, 2 Jexp time 3 Jenters, 3 JJ1 time exp 4 Jenters, tim 4 JJ2 time exp 5e Jenters, tim exp 518 time exp time exp 1 J4 Jfinishes exp 2 Jtime e3xp Jtime 4 J2 exp time exp time exp Q(a): Q6J5(a): Qtime QJ1(a): Q (a): QJ2 QJ4 Q(a): Q (a): (a): (a): Q(a): QJ3 Q (a): (a): Q(a): Q(a): Q Q (a): 5 Service 3(a): 4 (a): 2 Q(b): Q (b): Q(b): Q(b): QJ1 Q (b): Q(b): QJ4 QJ5 (b): QJ2 (b): Q(b): QJ3 (b): (b): (b): Q(b): Q(b): Q (b): Q (b): Wait time 8(c): 5J3, 5 Q(c): Q(c): Q(c): Q (c): QJ1 QJ1, QJ1, Q9J1, (c): Q J1, J2, J3, J4 9 (c): QJ1(c): QJ1, QJ1, J2 (c): (c): J2 J2, (c): J2, J3 J1, J2, (c): Q J3 (c): J2, Q J3, J2, (c): Q J3, J4 J3, (c): Q(c): J4 J4 J4J4 Q(d): Q(d):Q(d):Q Q(d): (d):Q(d): Q(d):Q(d): Q(d):Q(d):= Average respons e (d)L time =Q(11+1 8+12+1 3+2)/5 Q(d): (d): (d): (d): Q(d): Jcontinues 2,J2, JJ5 3witch J3, 3, 4, J4J2 J1 Jstarts Scontinues J2QScontinues Jto3QS J4J2S J5 S Switch S SJwitch SJwitch Switch witch to J1 1Q witch witch J2 continues witch to J3 continues witch to J4 to to to J2 J3 to J4 to to J2 J3 11.2 Feedback Queue Level = 4 1a 1b 2a 1c 1c 1c 1c 1c 1c 2b 3a 2c 2c 2c 2c 3b 4a 3c 3c 4b 1c 2c 3c 5a 4c 1c 2c 3c 4c 5b 1c 2c 3c 4c