UCLA CS111 Operating Systems (Spring 2003, Section 1) CPU Schedu ing
Instructor Andy Wang ([email protected]) Office: 3732J Boelter all Office ours: !"#3$ W"#2$ %&2#3$ and 'y a((oint)ent
*********************************************************************** *
!oa s "or a Schedu er
A scheduler &andles t&e re)o+al of t&e running (rocess fro) t&e ,-. and t&e selection of t&e ne/t running (rocess$ 'ased on a (articular strategy. A sc&eduler ai)s to acco)(lis& t&e following goals: ". !ini)i0e t&e wait time$ or t&e ti)e for a (rocess to recei+e its first unit of ser+ice fro) t&e (rocessor. (1ince t&e definition of res(onse ti)e &as different definitions across te/t'oo2s$ I use wait time t&roug&out )y course for consistency.) 2. !a/i)i0e t&e throughput$ or t&e nu)'er of co)(leted 3o's (er unit ti)e. %&ere are two (arts to )a/i)i0ing t&roug&(ut: a. !ini)i0e o+er&ead (e.g.$ conte/t switc&ing). '. 4fficient use of syste) resources (not only ,-.$ 'ut dis2$ )e)ory$ etc.). 3. Ac&ie+e fairness$ so t&e ,-. is s&ared a)ong users in a fair way. 5ote t&at t&ere are tensions a)ong t&ose t&ree goals.
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.
,learly$ t&ese assu)(tions are not realistic. 'e sol+ed.
owe+er$ t&ey si)(lify t&e (ro'le) so it can
Schedu ing Po icies
#irst $n, #irst Out (#$#O) %&e #$#O strategy assigns t&e (rocessor in t&e order of (rocessor re7uests. %&e 8I8O (olicy is nonpreemptive in t&e sense t&at a (rocess 2ee(s running on a ,-. until it is 'loc2ed or ter)inated. %&e 8I8O strategy is also 2nown as t&e #C#S (first co)e first ser+e) in 5utt9s te/t'oo2. -lease do go o+er 5utt9s e/a)(les in t&e 'oo2 for t&e actual co)(utation of t&e wait ti)e and t&e turnaround time (t&e total ti)e to co)(lete a (rocess9s e/ecution after it &as 'een )ade ready to e/ecute). %&e )ain ad+antage of t&e 8I8O (olicy is si)(licity. %&e 8I8O (olicy also (ro+ides a certain degree of fairness$ since re7uests are ser+ed in order. owe+er$ t&e )ain disad+antage is t&at s&ort 3o's can get stuc2 'e&ind long running 3o's. %ound %o&in (%%) %o o+erco)e t&e 8I8O (ro'le)$ t&e round-robin (olicy releases t&e ,-. fro) long# running 3o's (eriodically ('ased on ti)er interru(ts)$ so s&ort 3o's also can get a fair s&are of ,-.. %&erefore$ round ro'in is a preemptive (olicy. Just a'out e+ery real o(erating syste) does so)et&ing of t&is fla+or. %&e inter+al 'etween ti)er interru(ts is referred to as t&e time slice. After eac& ti)e slice$ t&e sc&eduler )o+es t&e (rocess to t&e 'ac2 of t&e 7ueue. ,&oosing t&e si0e of ti)e slice is tric2y. If t&e ti)e slice is too 'ig$ t&e wait ti)e suffers. If t&e si0e is too s)all$ t&roug&(ut suffers$ since t&e ,-. s(ends a lot of ti)e doing conte/t switc&ing. In (ractice$ we need to 'alance 'etween t&ese two scenarios. %y(ically$ a )odern o(erating syste) s(ends ": o+er&ead doing conte/t switc&ing. Comparison 'et(een #$#O and %ound %o&in Assu)ing 0ero#cost ti)e slice$ is ;; always 'etter t&an 8I8O< 5ot really. 1u((ose we &a+e "6 3o's$ eac& ta2es "66 seconds of ,-. ti)e$ and t&e ti)e slice for t&e round ro'in is " second. =et us loo2 at 3o' co)(letion ti)es.
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
Eueue " Eueue 2 Eueue 3 Eueue F
!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: