Lecture 4 Cpu Scheduling
Lecture 4 Cpu Scheduling
Instructor Andy Wang ([email protected]) Office: 3732J Boelter all Office ours: !"#3$ W"#2$ %&2#3$ and 'y a((oint)ent
*********************************************************************** *
Assumptions
1c&eduling is a well#studied area. In fact$ 'ac2 in t&e early 76s$ sc&eduling was a 'ig area of researc&. ,o))on studies )a2e t&e following assu)(tions: ". 4ac& user runs one (rocess. 2. 4ac& (rocess is single t&readed. 3. -rocesses are inde(endent.
Jo' ,o)(letion %i)es Jo' > 8I8O ;ound ro'in " "66 ??" 2 266 ??2 @ @ @ ? ?66 ??? "6 "666 "666 Alt&oug& 'ot& round ro'in and 8I8O finis& at t&e sa)e ti)e$ t&e a+erage turnaround ti)e is )uc& worse under round ro'in t&an under 8I8O. %&erefore$ round ro'in is 'etter for s&ort 3o's$ 'ut it is (oor for 3o's t&at are t&e sa)e lengt&. S)C#*S%)C# S)C# (s&ortest ti)e to co)(letion first) runs w&ate+er 3o' &as t&e least de)and on ,-.. %&e 1%,8 is also 2nown as S+, (s&ortest 3o' ne/t) or S+# (s&ortest 3o' first). S%)C# (s&ortest re)aining ti)e to co)(letion first) is 3ust a (ree)(ti+e +ersion of 1%,8. If a 3o' arri+es t&at &as a s&orter ti)e to co)(letion t&an t&e re)aining ti)e on t&e current 3o'$ 1;%,8 i))ediately (ree)(ts t&e ,-. for t&e new 3o'. %&e idea is to get s&ort 3o's out of t&e syste). We can see a 'ig i)(ro+e)ent on turnaround ti)es for s&ort 3o's$ and a relati+ely s)all degradation of turnaround ti)es for larger 3o's$ resulting in a 'etter a+erage turnaround ti)e. In fact$ 1%,8 and 1;%,8 are t&e 'est you can (ossi'ly do at )ini)i0ing t&e turnaround ti)e. Comparison o" S%)C# (ith #$#O and %ound %o&in If all 3o's are t&e sa)e lengt&$ 1;%,8 'eco)es t&e sa)e as 8I8O. In ot&er words$ in t&at instance$ 8I8O is as good as you can do. .nder 1;%,8 if 3o's &a+e +arying lengt&$ s&ort 3o's do not get stuc2 'e&ind long 3o's. %&e following is an e/a)(le t&at illustrates t&e 'enefit of 1;%,8. 1u((ose we &a+e t&ree 3o's: A$ B$ and ,. A and B are 'ot& ,-. 'ound$ and eac& runs for a wee2. Jo' , consists of an IAO 'ound loo($ w&ic& consu)es " )sec of ,-. and "6 )sec of dis2 IAO (assu)ing co)(lete o+erla((ing of IAO and ,-. (rocessing). By itself$ , uses ?6: of t&e dis2. By itself$ A or B could use "66: of t&e ,-.. W&at &a((ens if we try to s&are t&e syste) a)ong A$ B$ and ,< .nder 8I8O$ A and B will ta2e two wee2s 'efore getting to 3o' ,.
.nder round ro'in wit& "66 )sec of ti)e slice$ we can only get B: of dis2 utili0ation$ since 3o' , uses dis2 for "6 )sec of e+ery 26" )sec. ,-. IAO owe+er$ if we reduce t&e ti)e slice down to " )sec$ we can get ?6: dis2 utili0ation againCal)ost as good as 3o' , running alone. Also$ we &a+e not slowed down A or B 'y all t&at )uc&D t&ey will get ?6: of t&e ,-.. , (" )sec) ,-. AB,AB AB AB AB AB IAO .nder 1;%,8$ 3o' , is run as soon as (ossi'le$ t&en A or B is run to co)(letion. ,-. IAO O+erall$ t&e ad+antage of t&e 1;%,8 is t&at it ac&ie+es t&e o(ti)al a+erage turnaround ti)e. owe+er$ 1;%,8 can lead to t&e (ossi'ility of starvation$ w&ere lots of s&ort 3o's can 2ee( long 3o's fro) )a2ing (rogress. In addition to unfair treat)ent of long 3o's$ 1;%,8 de(ends on (redicting t&e ,-. running ti)e for +arious 3o's. One way to (redict t&e lengt& of a 3o' is 'y as2ing t&e user. If a user c&eats (if a 3o' ta2es longer t&an s(ecified)$ t&e 3o' is 2illed. owe+er$ t&e user )ay not 2now any 'etter. 1;%,8 is generally used as t&e o(ti)al case for co)(aring +arious sc&eduling algorit&)s. -u ti e.e #eed&ac/ 0ueues One way to (redict t&e future is fro) t&e e/isting 'e&a+ior of (rocesses. owe+er$ since a (rocess )ay switc& 'etween ,-.#'ound and IAO#'ound 'e&a+iors$ a sc&eduling (olicy ideally can ada(t c&anging 'e&a+iors 'ased on recent 'e&a+iors. , A , A A ("66 )sec) B , A B
Multilevel feedback queues use )ulti(le 7ueues$ eac& wit& a different (riority. An o(erating syste) does a round ro'in at eac& (riority le+elCrun &ig&est (riority 3o's firstD once t&ose finis&$ run ne/t &ig&est (riority$ etc. A round#ro'in ti)e slice increases e/(onentially at lower (riorities. -riority %i)e slice " " 2 2 3 F F G
!ultile+el feed'ac2 7ueues ad3ust eac& 3o'9s (riority as follows: ". Jo' starts in t&e &ig&est (riority 7ueue. 2. If ti)e slice e/(ires$ dro( t&e 3o' 'y one le+el. 3. If ti)e slice does not e/(ire$ (us& t&e 3o' u( 'y one le+el. %&e resulting algorit&) a((ro/i)ates 1;%,8: ,-.#'ound 3o's dro( li2e a roc2$ w&ile s&ort#running IAO#'ound 3o's stay near t&e to(. %&e )ultile+el feed'ac2 7ueues are still unfair for long running 3o's. 1o)e (rogra)s )ay s(oradically generate )eaningless IAOs 3ust to 2ee( t&e 3o'9s (riority &ig&. !ultile+el feed'ac2 7ueues are (oor in &andling situations li2e &a+ing one long#running 3o' and "66 s&ort#running ones. .5IH tries to age$ or increase t&e (riority of$ long# running 3o's if t&ey are not ser+iced for a (eriod of ti)e. owe+er$ t&e rate of aging 3o's is difficult to deter)ine. Also$ for an o+erloaded syste) w&ere no 3o' can 'e ser+iced$ e+ery 3o' increases in (riority$ and interacti+e s&ort 3o's 'egin to suffer as well. Lottery Schedu ing Lottery scheduling is anot&er ada(ti+e sc&eduling a((roac& t&at addresses t&e fairness (ro'le) of )ultile+el feed'ac2 7ueues. %&e lottery sc&eduler gi+es e+ery 3o' so)e nu)'er of lottery tic2ets$ and on eac& ti)e slice$ it rando)ly (ic2s a winning tic2et. On a+erage$ t&e ,-. ti)e allotted is (ro(ortional to t&e nu)'er of tic2ets gi+en to eac& 3o'. %o a((ro/i)ate 1;%,8$ s&ort 3o's get )ore tic2ets$ and long running 3o's get fewer. %o a+oid star+ation$ e+ery 3o' gets at least one tic2et (so e+eryone )a2es (rogress).
=ottery sc&eduling 'e&a+es gracefully as loads c&anges. Adding or deleting a 3o' affects all 3o's (ro(ortionally$ inde(endent of &ow )any tic2ets eac& 3o' &as. 8or e/a)(le$ if s&ort 3o's get "6 tic2ets$ and long 3o's get " eac&$ for different scenarios listed 'elow$ we can get )eaningful 'e&a+ior for all. > s&ort 3o'sA > long 3o's "A" 6A2 2A6 "6A" "A"6 : of ,-. for eac& s&ort 3o' ?": 5AA B6: "6: B6: : of ,-. for eac& long 3o' ?: B6: 5AA ": B: