资源描述:
《nachos实验9设计并实现具有优先级的线程调度策略》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、实验目的Nachos系统采用基木的先來先服务的线程调度策略,木次试验的目的:熟悉Nachos涼打的线程调度策略设计并实现具打优先级的线程调度策略实验环境linux操作系统,Nachos操作系统实验分析将原冇的先來先服务的线程调度策略改力按优先级调度的策略,那么每个线程需添加屈性priority,决定当前线程的优先级髙低。定义优先级取值范围为1-7,其中1为敁髙,7为蝻低。那么在当前线程被阻塞的时候,调度蚋数通过判断当前就绪队列中的线程中谁的优先级敁髙,就调度谁,使其运行。更力优化的办法足,每次将一个线程插入的就绪队列中时,就按照线程的优先级
2、顺序插入,让List中的等待时间片的线程依照优先级从髙到低排好序,那么每次齊询下一耍执行的线程的时候,无需再遍历List,直接从头指针卜.截取即可,如此插入和移除的平均时间将节省一半。关键源代码及注释threadtest.cc改写涼冇的测试蚋数////ThreadTest//@Lizhen11/16/09//Setupafewthreadswithprioritykeys,byforkingathread//tocalloriginalSimpleThreadtoseethethreadchoosedbypriority.//Priorit
3、ykeymustbechoosenbetween1and7.//Thelowestis7andhighestis1.//voidThreadTest(){DEBUG('t',"EnteringSimpleTest");Thread*t=newThread(n1",5);//线程1,优先级为5*Thread*t2=newThread("2”,1);Thread*t3=newThread("3”,3);t->Fork(SimpleThread,1);t2->Fork(SimpleThread,2);t3->Fork(SimpleThread,3
4、);}*优先级取值范围:1-7,1为敁髙,7为敁低。thread.h和thread.cc力丫实现根据线程thread按照优先级进行调度,在Thread类中增加参数priority,优先级取伹范围为1至7,1为敁高,7为敁低。因力整个系统中必然会冇许多地7/都使川丫Thread,耍将这所有的地7/找出來并增加线程优先级似乎足不现实的,更改后系统的TF.确性和稳定性也足受质疑的,力丫私敁人限度地不改动源代码,不删除原冇的构造闲数,改写为不给优先级赋值时默认优先级为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