nachos java_Nachos java版学习(一)
最近,操作系統(tǒng)課程設(shè)計(jì)使用伯克利大學(xué)的Nachos做為實(shí)驗(yàn)平臺(tái),老師也照搬伯克利的Project要求,開(kāi)始我們的操作系統(tǒng)課程設(shè)計(jì)。
結(jié)合自己的學(xué)習(xí)過(guò)程和課設(shè)要求,我覺(jué)得對(duì)Nachos的學(xué)習(xí)首先應(yīng)該從KThread.java入手,首先應(yīng)該看明白這個(gè)類的所有函數(shù)的意思。
這個(gè)主要是為第一個(gè)proj,實(shí)現(xiàn)join()函數(shù)做準(zhǔn)備。Join()方法的含義:當(dāng)前線程a在運(yùn)行,執(zhí)行b.join(),則a阻塞,直到線程b結(jié)束,a繼續(xù)執(zhí)行。
具體的要求有:Join函數(shù)的作用即為等待某線程運(yùn)行完畢。當(dāng)前線程 (唯一一個(gè)正在運(yùn)行的線程) A調(diào)用另一個(gè)線程 (處于就緒狀態(tài)) B的join函數(shù)時(shí) (A 和 B 在Nachos中均為KThread類型對(duì)象),A被掛起,直到B運(yùn)行結(jié)束后, join函數(shù)返回,A才能繼續(xù)運(yùn)行。注意在一個(gè)KThread對(duì)象上只能調(diào)用一次join,且當(dāng)前線程不能對(duì)自身調(diào)用join。Waits for this thread to finish. If this thread is already finished, return immediately. This method must only be called once; the second call is not guaranteed to return. This thread must not be the current thread.
1 public voidjoin() {2
3 Lib.debug(dbgThread, "Joining to thread: " +toString());4 //等待另外一個(gè)線程結(jié)束的這個(gè)線程不能是線程自己
5 Lib.assertTrue(this !=currentThread);6
7 if (this.status ==statusFinished)8 return;9
10 boolean intStatus =Machine.interrupt().disable();11
12 if (joinQueue == null) {13 joinQueue = ThreadedKernel.scheduler.newThreadQueue(true);14 //Notify this thread queue that a thread has received access,without going through request() and nextThread()15 joinQueue.acquire(this);17 }18 //Notify this thread queue that the specified thread is waiting for19 //access
20 joinQueue.waitForAccess(currentThread);21 KThread.sleep();22 Machine.interrupt().restore(intStatus);23 }
為此,我設(shè)置了一個(gè)KThread的隊(duì)列joinQueue,當(dāng)B調(diào)用join函數(shù)時(shí),將正在執(zhí)行的A線程放入joinQueue,只有在B執(zhí)行完后,在finish()里面會(huì)將隊(duì)列中的A線程狀態(tài)變?yōu)閞eady,從而繼續(xù)執(zhí)行A線程,具體finish()實(shí)現(xiàn)的代碼如下:
1 public static voidfinish() {2 Lib.debug(dbgThread, "Finishing thread: " +currentThread.toString());3
4 Machine.interrupt().disable();5
6 Machine.autoGrader().finishingCurrentThread();7
8 Lib.assertTrue(toBeDestroyed == null);9 toBeDestroyed =currentThread;10
11 currentThread.status =statusFinished;12
13 KThread joinedKThread;14 if (currentThread.joinQueue != null)15 while ((joinedKThread = currentThread.joinQueue.nextThread()) != null)16 joinedKThread.ready();17 sleep();18 }
后續(xù)測(cè)試程序如下:
packagenachos.threads;import nachos.machine.*;public classKThreadTest {publicKThreadTest() {
}public static voidsimpleJoinTest() {
KThread A_thread= new KThread(new KThreadTest.A_thread(5));
KThread B_thread= new KThread(newKThreadTest.B_thread(A_thread));
B_thread.fork();
B_thread.join();
}public static class B_thread implementsRunnable {
B_thread(KThread joinee) {this.joinee =joinee;
}public voidrun() {
System.out.println("B_thread 就緒");
System.out.println("Forking and joining A_thread...");this.joinee.fork();//Causes this thread to begin execution.
this.joinee.join();//啟動(dòng)a線程,阻塞b線程
System.out.println("B_thread 執(zhí)行結(jié)束");
}privateKThread joinee;
}public static class A_thread implementsRunnable {
A_thread(intnum) {this.num =num;
}public voidrun() {
System.out.println("A_thread 就緒");
System.out.println("A_thread開(kāi)始執(zhí)行");//This should just kill some cycles
for (int i = 0; i < this.num; ++i) {
System.out.println("A_thread 循環(huán) 第" + i + " 次");
KThread.currentThread().yield();
}
System.out.println("A_thread 執(zhí)行結(jié)束");
}private intnum;
}
}
總結(jié)
以上是生活随笔為你收集整理的nachos java_Nachos java版学习(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CSS3常用属性及用法
- 下一篇: 面试必备:HashMap、Hashtab