資源描述:
《nachos實驗9設(shè)計并實現(xiàn)具有優(yōu)先級的線程調(diào)度策略》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在學(xué)術(shù)論文-天天文庫。
1、實驗?zāi)康腘achos系統(tǒng)采用基木的先來先服務(wù)的線程調(diào)度策略,木次試驗的目的:熟悉Nachos涼打的線程調(diào)度策略設(shè)計并實現(xiàn)具打優(yōu)先級的線程調(diào)度策略實驗環(huán)境linux操作系統(tǒng),Nachos操作系統(tǒng)實驗分析將原冇的先來先服務(wù)的線程調(diào)度策略改力按優(yōu)先級調(diào)度的策略,那么每個線程需添加屈性priority,決定當(dāng)前線程的優(yōu)先級髙低。定義優(yōu)先級取值范圍為1-7,其中1為敁髙,7為蝻低。那么在當(dāng)前線程被阻塞的時候,調(diào)度蚋數(shù)通過判斷當(dāng)前就緒隊列中的線程中誰的優(yōu)先級敁髙,就調(diào)度誰,使其運行。更力優(yōu)化的辦法足,每次將一個線程插入的就緒隊列中時,就按照線程的優(yōu)先級
2、順序插入,讓List中的等待時間片的線程依照優(yōu)先級從髙到低排好序,那么每次齊詢下一耍執(zhí)行的線程的時候,無需再遍歷List,直接從頭指針卜.截取即可,如此插入和移除的平均時間將節(jié)省一半。關(guān)鍵源代碼及注釋threadtest.cc改寫涼冇的測試蚋數(shù)////ThreadTest//@Lizhen11/16/09//Setupafewthreadswithprioritykeys,byforkingathread//tocalloriginalSimpleThreadtoseethethreadchoosedbypriority.//Priorit
3、ykeymustbechoosenbetween1and7.//Thelowestis7andhighestis1.//voidThreadTest(){DEBUG('t',"EnteringSimpleTest");Thread*t=newThread(n1",5);//線程1,優(yōu)先級為5*Thread*t2=newThread("2”,1);Thread*t3=newThread("3”,3);t->Fork(SimpleThread,1);t2->Fork(SimpleThread,2);t3->Fork(SimpleThread,3
4、);}*優(yōu)先級取值范圍:1-7,1為敁髙,7為敁低。thread.h和thread.cc力丫實現(xiàn)根據(jù)線程thread按照優(yōu)先級進行調(diào)度,在Thread類中增加參數(shù)priority,優(yōu)先級取伹范圍為1至7,1為敁高,7為敁低。因力整個系統(tǒng)中必然會冇許多地7/都使川丫Thread,耍將這所有的地7/找出來并增加線程優(yōu)先級似乎足不現(xiàn)實的,更改后系統(tǒng)的TF.確性和穩(wěn)定性也足受質(zhì)疑的,力丫私敁人限度地不改動源代碼,不刪除原冇的構(gòu)造閑數(shù),改寫為不給優(yōu)先級賦值時默認優(yōu)先級為7。.hpublic:Thread(char*debugName,intprior
5、ity=7);//initializeaThread"@LiZhen11/11/09//Ifdonotinitializethepriority,//givethelowestprioritytoit.//Priorityragefrom1to7.voidYield();"RelinquishtheCPUifany//otherthreadisrunnable//@LiZhen11/11/09////Thread::Thread//Initializeathreadcontrolblock,sothatwecanthencall//Thre
6、ad::Fork.////"threadName"isanarbitrarystring,usefulfordebugging.////@LiZhen11/11/09//modifiedbyLiZhenatNov11,2009Thread::Thread(char*threadName,intp)if(p<1)priority=1;elseif(p>7)priority=7;elsepriority=p;name=threadName;stackhp=NULL;stack=NULL;status=JUST_CREATED;#ifdefUSE
7、R_PROGRAMspace=NULL;#endif}////Thread::Yield//RelinquishtheCPUifanyotherthreadisreadytorun.//Ifso,putthethreadontheendofthereadylist,sothat//itwilleventuallybere-scheduled.////NOTE:returnsimmediatelyifnootherthreadonthereadyqueue.//Otherwisereturnswhenthethreadeventuallywo
8、rksitsway//tothefrontofthereadylistandgetsre-scheduled.////NOTE:wedisableinterrupts,sotha