操作系统课程设计nachosproject1源代码

操作系统课程设计nachosproject1源代码

ID:25467063

大小:219.50 KB

页数:29页

时间:2018-11-20

操作系统课程设计nachosproject1源代码_第1页
操作系统课程设计nachosproject1源代码_第2页
操作系统课程设计nachosproject1源代码_第3页
操作系统课程设计nachosproject1源代码_第4页
操作系统课程设计nachosproject1源代码_第5页
资源描述:

《操作系统课程设计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()

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。