操作系统课程设计nachosProject1源代码506.pdf
《操作系统课程设计nachosProject1源代码506.pdf》由会员分享,可在线阅读,更多相关《操作系统课程设计nachosProject1源代码506.pdf(55页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Task1 修改 KThread package nachos.threads;import nachos.machine.*;public class KThread public KThread(Runnable target)this();/调用无参数的构造器 this.target=target;public KThread()boolean status=Machine.interrupt().disable();if(currentThread!=null)tcb=new TCB();else currentThread=this;tcb=TCB.currentTCB();/第一个
2、线程是主线程,指向第一个 TCB name=main;readyQueue=ThreadedKernel.scheduler.newThreadQueue(false);readyQueue.acquire(this);restoreState();/对主线程置运行状态 createIdleThread();waitQueue.acquire(this);Machine.interrupt().restore(status);public void fork()/执行 KThread Lib.assertTrue(status=statusNew);Lib.assertTrue(target!
3、=null);Lib.debug(dbgThread,Forking thread:+toString()+Runnable:+target);boolean intStatus=Machine.interrupt().disable();/关中断,在线程将要执行的准备阶段不能被打断 tcb.start(new Runnable()public void run()runThread();/方法内有 target );ready();/并未真正开始执行,只是将线程移动到 ready 队列 Machine.interrupt().restore(intStatus);/回到机器原来的状态,就好像
4、生成线程这件事从没发生过 private void runThread()begin();target.run();/执行 target finish();private void begin()Lib.debug(dbgThread,Beginning thread:+toString();Lib.assertTrue(this=currentThread);restoreState();Machine.interrupt().enable();/开中断 public static void finish()Lib.debug(dbgThread,Finishing thread:+curr
5、entThread.toString();Machine.interrupt().disable();/关中断 Machine.autoGrader().finishingCurrentThread();/将 TCB 变成将要结束的 TCB Lib.assertTrue(toBeDestroyed=null);toBeDestroyed=currentThread;/将当前线程变为将要结束的线程,下一个线程运行的时候自动消除它 currentThread.status=statusFinished;/当前线程状态置为完成 KThread thread=currentThread().waitQ
6、ueue.nextThread();if(thread!=null)thread.ready();sleep();/将当前线程置为完成态,读取下一个就绪线程 public static void sleep()/如果线程执行完,则是从 finish 来,否则线程锁死,读取下一个线程 Lib.debug(dbgThread,Sleeping thread:+currentThread.toString();Lib.assertTrue(Machine.interrupt().disabled();if(currentThread.status!=statusFinished)currentThr
7、ead.status=statusBlocked;runNextThread();private static void runNextThread()/执行下一个线程 KThread nextThread=readyQueue.nextThread();if(nextThread=null)nextThread=idleThread;/如果线程队列为空则执行 idle 线程nextThread.run();private void run()Lib.assertTrue(Machine.interrupt().disabled();Machine.yield();/当前 java 线程放弃
8、CPU currentThread.saveState();/无实际操作 Lib.debug(dbgThread,Switching from:+currentThread.toString()+to:+toString();currentThread=this;tcb.contextSwitch();currentThread.restoreState();public static void yield()/运行线程放弃 cpu,将当前线程放入就绪队列,读取就绪队列下一个线程运行 Lib.debug(dbgThread,Yielding thread:+currentThread.toSt
9、ring();Lib.assertTrue(currentThread.status=statusRunning);boolean intStatus=Machine.interrupt().disable();currentThread.ready();/正在执行的线程放入就绪队列,执行就绪队列的下一个线程 runNextThread();/运行下一个线程 Machine.interrupt().restore(intStatus);public void ready()/将线程移动到 ready 队列 Lib.debug(dbgThread,Ready thread:+toString()
10、;Lib.assertTrue(Machine.interrupt().disabled();Lib.assertTrue(status!=statusReady);status=statusReady;if(this!=idleThread)readyQueue.waitForAccess(this);/将线程移入队列,idle 线程不用放入等待队列 Machine.autoGrader().readyThread(this);/空方法 private static void createIdleThread()/创建 idle 线程 Lib.assertTrue(idleThread=nu
11、ll);idleThread=new KThread(new Runnable()public void run()while(true)yield();/idle 线程一直执行的操作是 yield(放弃);idleThread.setName(idle);Machine.autoGrader().setIdleThread(idleThread);/空方法 idleThread.fork();protected void restoreState()/恢复状态,执行此线程,如果有要结束的线程就结束它 Lib.debug(dbgThread,Running thread:+currentThr
12、ead.toString();Lib.assertTrue(Machine.interrupt().disabled();Lib.assertTrue(this=currentThread);Lib.assertTrue(tcb=TCB.currentTCB();Machine.autoGrader().runningThread(this);status=statusRunning;if(toBeDestroyed!=null)toBeDestroyed.tcb.destroy();toBeDestroyed.tcb=null;toBeDestroyed=null;public void j
13、oin()/线程 B 中有 A.join()语句,则 B 等 A 执行完才能执行 Lib.debug(dbgThread,Joining to thread:+toString();Lib.assertTrue(this!=currentThread);Lib.assertTrue(join_counter=0);join_counter+;boolean status=Machine.interrupt().disable();if(this.status!=statusFinished)waitQueue.waitForAccess(KThread.currentThread();curr
14、entThread.sleep();Machine.interrupt().restore(status);public static KThread currentThread()/返回当前 KThread Lib.assertTrue(currentThread!=null);return currentThread;private static class PingTest implements Runnable PingTest(int which)this.which=which;public void run()for(int i=0;i 5;i+)System.out.print
15、ln(*thread +which+looped +i +times);currentThread.yield();/每次执行一次就让权 private int which;public static void selfTest()/检测是否工作正常 Lib.debug(dbgThread,Enter KThread.selfTest);/new KThread(new PingTest(1).setName(forked thread).fork();/new PingTest(0).run();/selfTest_join();/selfTest_Condition2();/selfTes
16、t_Alarm();/selfTest_Communicator();/selfTest_Boat();/selftest_Scheduler();selftest_LotteryScheduler();public static void selfTest_join()/检测 join 是否工作正常 Lib.debug(dbgThread,Enter KThread.selfTest);System.out.println(_join test begin_);final KThread thread1=new KThread(new PingTest(1);thread1.setName(
17、forked thread).fork();new KThread(new Runnable()public void run()thread1.join();currentThread.yield();System.out.println(successful);).fork();public static void selfTest_Condition2()/检测 Condition2 是否工作正常 Lib.debug(dbgThread,Enter KThread.selfTest);System.out.println(_Condition2 test begin_);final Lo
18、ck lock=new Lock();final Condition2 condition2=new Condition2(lock);new KThread(new Runnable()public void run()lock.acquire();/线程执行之前获得锁 KThread.currentThread().yield();condition2.sleep();System.out.println(thread1 executing);condition2.wake();lock.release();/线程执行完毕将锁释放 System.out.println(thread1 ex
19、ecute successful);).fork();new KThread(new Runnable()public void run()lock.acquire();/线程执行之前获得锁 KThread.currentThread().yield();condition2.wake();System.out.println(thread2 executing);condition2.sleep();lock.release();/线程执行完毕将锁释放 System.out.println(thread2 execute successful);).fork();public static
20、void selfTest_Alarm()/检测 Alarm 是否工作正常 new KThread(new Runnable()public void run()System.out.println(Machine.timer().getTime();ThreadedKernel.alarm.waitUntil(500);System.out.println(Machine.timer().getTime();System.out.println(successful);).fork();public static void selfTest_Communicator()/检测 Communi
21、cator 是否工作正常 Lib.debug(dbgThread,Enter KThread.selfTest);System.out.println(_Communicator test begin_);final Communicator communicator=new Communicator();new KThread(new Runnable()public void run()communicator.speak(20);System.out.println(thread1 successful);).fork();new KThread(new Runnable()public
22、 void run()communicator.speak(30);System.out.println(thread2 successful);).fork();new KThread(new Runnable()public void run()System.out.println(communicator.listen();System.out.println(thread3 successful);).fork();new KThread(new Runnable()public void run()System.out.println(communicator.listen();Sy
23、stem.out.println(thread4 successful);).fork();public static void selfTest_Boat()/检测 join 是否工作正常Lib.debug(dbgThread,Enter KThread.selfTest);System.out.println(_Boat test begin_);new KThread(new Runnable()public void run()Boat.selfTest();System.out.println(successful);).fork();public static void selft
24、est_Scheduler()final KThread thread1=new KThread(new Runnable()public void run()for(int i=0;i3;i+)KThread.currentThread().yield();System.out.println(thread1););KThread thread2=new KThread(new Runnable()public void run()for(int i=0;i3;i+)KThread.currentThread().yield();System.out.println(thread2););K
25、Thread thread3=new KThread(new Runnable()public void run()thread1.join();for(int i=0;i3;i+)KThread.currentThread().yield();System.out.println(thread3););boolean status=Machine.interrupt().disable();ThreadedKernel.scheduler.setPriority(thread1,2);ThreadedKernel.scheduler.setPriority(thread2,4);Thread
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 课程设计 nachosProject1 源代码 506
限制150内