Finished Part 4 with naive testing
parent
0bf8f95596
commit
b699a736cc
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -9,11 +9,13 @@ import nachos.machine.*;
|
|||
* be a time when both a speaker and a listener are waiting, because the two
|
||||
* threads can be paired off at this point.
|
||||
*/
|
||||
public class Communicator {
|
||||
public class Communicator
|
||||
{
|
||||
/**
|
||||
* Allocate a new communicator.
|
||||
*/
|
||||
public Communicator() {
|
||||
public Communicator()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,7 +28,26 @@ public class Communicator {
|
|||
*
|
||||
* @param word the integer to transfer.
|
||||
*/
|
||||
public void speak(int word) {
|
||||
public void speak(int word)
|
||||
{
|
||||
condLock.acquire();
|
||||
if(protect==1) condProtect.sleep();
|
||||
speakers++;
|
||||
protect=1;
|
||||
if(listeners==0)
|
||||
{
|
||||
condSpeaker.sleep();
|
||||
message=word;
|
||||
condPaired.wake();
|
||||
}
|
||||
else
|
||||
{
|
||||
message=word;
|
||||
listeners--;
|
||||
speakers--;
|
||||
condListener.wake();
|
||||
}
|
||||
condLock.release();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,7 +56,34 @@ public class Communicator {
|
|||
*
|
||||
* @return the integer transferred.
|
||||
*/
|
||||
public int listen() {
|
||||
return 0;
|
||||
public int listen()
|
||||
{
|
||||
int ret=0;
|
||||
condLock.acquire();
|
||||
listeners++;
|
||||
if(speakers==0) condListener.sleep();
|
||||
else
|
||||
{
|
||||
speakers--;
|
||||
listeners--;
|
||||
condSpeaker.wake();
|
||||
condPaired.sleep();
|
||||
}
|
||||
ret=message;
|
||||
protect=0;
|
||||
condProtect.wake();
|
||||
condLock.release();
|
||||
return ret;
|
||||
}
|
||||
|
||||
private int speakers=0;
|
||||
private int listeners=0;
|
||||
private int protect=0;
|
||||
private int message=0;
|
||||
private Lock condLock=new Lock();
|
||||
private Condition condProtect=new Condition(condLock);
|
||||
private Condition condSpeaker=new Condition(condLock);
|
||||
private Condition condListener=new Condition(condLock);
|
||||
private Condition condPaired=new Condition(condLock);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package nachos.threads;
|
||||
|
||||
import nachos.machine.*;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* A KThread is a thread that can be used to execute Nachos kernel code. Nachos
|
||||
|
@ -500,6 +501,44 @@ public class KThread {
|
|||
private int duration;
|
||||
}
|
||||
|
||||
private static class ComTest implements Runnable
|
||||
{
|
||||
ComTest(int id,Communicator com)
|
||||
{
|
||||
this.id=id;
|
||||
this.com=com;
|
||||
}
|
||||
public void run()
|
||||
{
|
||||
if(id<=48)
|
||||
{
|
||||
Random rand=new Random();
|
||||
while(true)
|
||||
{
|
||||
int x=rand.nextInt(100);
|
||||
System.out.println("Thread "+id+" speaking: "+x);
|
||||
com.speak(x);
|
||||
System.out.println("Thread "+id+" finished speaking: "+x);
|
||||
ThreadedKernel.alarm.waitUntil(10000000);
|
||||
//System.out.println("Thread "+id+" slept for "+(after-before)+" ticks");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
System.out.println("Thread "+id+" listening");
|
||||
int x=com.listen();
|
||||
System.out.println("Thread "+id+" received "+x);
|
||||
ThreadedKernel.alarm.waitUntil(10000000);
|
||||
//System.out.println("Thread "+id+" slept for "+(after-before)+" ticks");
|
||||
}
|
||||
}
|
||||
}
|
||||
private int id;
|
||||
private Communicator com;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether this module is working.
|
||||
*/
|
||||
|
@ -525,11 +564,18 @@ public class KThread {
|
|||
*/
|
||||
|
||||
//Test for waitUntil
|
||||
/*
|
||||
new KThread(new AlarmTest(1,10000000)).fork();
|
||||
new KThread(new AlarmTest(2,10000000)).fork();
|
||||
new KThread(new AlarmTest(3,10000000)).fork();
|
||||
new KThread(new AlarmTest(4,10000000)).fork();
|
||||
new AlarmTest(0,50000000).run();
|
||||
*/
|
||||
|
||||
//Test for Communicator
|
||||
Communicator com=new Communicator();
|
||||
for(int i=1;i<50;i++) new KThread(new ComTest(i,com)).fork();
|
||||
new ComTest(0,com).run();
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue