欢迎来到天天文库
浏览记录
ID:25467063
大小:219.50 KB
页数:29页
时间:2018-11-20
《操作系统课程设计nachosproject1源代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Task1修改KThreadpackagenachos.threads;importnachos.machine.*;publicclassKThread{publicKThread(Runnabletarget){this();//调用无参数的构造器this.target=target;}publicKThread(){booleanstatus=Machine.interrupt().disable();if(currentThread!=null){tcb=newTCB();}else{currentThread=this;tcb=TCB.currentTCB();//第一个线程是
2、主线程,指向第一个TCBname="main";readyQueue=ThreadedKernel.scheduler.newThreadQueue(false);readyQueue.acquire(this);restoreState();//对主线程置运行状态createIdleThread();}waitQueue.acquire(this);Machine.interrupt().restore(status);}publicvoidfork(){//执行KThreadLib.assertTrue(status==statusNew);Lib.assertTrue(target
3、!=null);Lib.debug(dbgThread,"Forkingthread:"+toString()+"Runnable:"+target);booleanintStatus=Machine.interrupt().disable();//关中断,在线程将要执行的准备阶段不能被打断tcb.start(newRunnable(){publicvoidrun(){runThread();//方法内有target}});ready();//并未真正开始执行,只是将线程移动到ready队列Machine.interrupt().restore(intStatus);//回到机器原来的状
4、态,就好像生成线程这件事从没发生过}privatevoidrunThread(){begin();target.run();//执行targetfinish();}privatevoidbegin(){Lib.debug(dbgThread,"Beginningthread:"+toString());Lib.assertTrue(this==currentThread);restoreState();Machine.interrupt().enable();//开中断}publicstaticvoidfinish(){Lib.debug(dbgThread,"Finishingthre
5、ad:"+currentThread.toString());Machine.interrupt().disable();//关中断Machine.autoGrader().finishingCurrentThread();//将TCB变成将要结束的TCBLib.assertTrue(toBeDestroyed==null);toBeDestroyed=currentThread;//将当前线程变为将要结束的线程,下一个线程运行的时候自动消除它currentThread.status=statusFinished;//当前线程状态置为完成KThreadthread=currentThre
6、ad().waitQueue.nextThread();if(thread!=null){thread.ready();}sleep();//将当前线程置为完成态,读取下一个就绪线程}publicstaticvoidsleep(){//如果线程执行完,则是从finish来,否则线程锁死,读取下一个线程Lib.debug(dbgThread,"Sleepingthread:"+currentThread.toString());Lib.assertTrue(Machine.interrupt().disabled());if(currentThread.status!=statusFini
7、shed)currentThread.status=statusBlocked;runNextThread();}privatestaticvoidrunNextThread(){//执行下一个线程KThreadnextThread=readyQueue.nextThread();if(nextThread==null)nextThread=idleThread;//如果线程队列为空则执行idle线程nextThread.run()
此文档下载收益归作者所有