We use the nachos java version 5.0 to test the practical assignments
For this presentation we've implemented a runnable program that shows the use of locks on the unbounded-buffer consumer-producer problem.
I didn't use any slide, i only showed my code to the classroom, compiled it and finally, execute my program.
On the Lock code, i used the one that nachos provide in his tar, and i just add two lines to check the execution without debug parameters.
These are the lines that i added:
in the acquire function
System.out.println(" acquire successful by thread: "+ KThread.currentThread());
in the release function
System.out.println(" release successful by thread: "+ KThread.currentThread());
the link to the Locks source code is here
(also you can check on /nachos/threads/Locks.java)
This is the code that i wrote:
package nachos.threads;
import nachos.threads.*;
public class Prueba{
private static Lock l = null;
private static Lock l1 = null;
private static int in_stock = 0;
public static class Producer implements Runnable{
private KThread currentThread = null;
public Producer(){ // constructor
}
public void run(){ //metodo run, lo que hace mi programa
System.out.println("\nThe Producer start...");
l.acquire();
in_stock++;
System.out.println("the producer make 1 item, now there are "+in_stock+" in stock.");
l.release();
this.currentThread.yield();
}
}
public static class Consumer implements Runnable{
private KThread currentThread = null;
public Consumer(){ //constructor
}
public void run(){ //metodo run, lo que hace mi programa
System.out.println("\nThe Consumer start...");
l.acquire();
while(in_stock <= 0){
currentThread.sleep(); }
l1.acquire();
in_stock--;
l1.release();
System.out.println("the consumer take 1 item, now there are "+in_stock+" in stock.");
l.release();
this.currentThread.yield();
}
}
public static void start() {
l = new Lock();
l1 = new Lock();
while(true){
try{
Thread.currentThread().sleep(2000);
} catch(Exception e){ }
new KThread(new Producer()).setName("Producer").fork();
new KThread(new Consumer()).setName("Consumer").fork();
new Producer().run();
new Consumer().run(); }
}
}
Finally, i modify the nachos code because the "*** thread 'x' looped 'x' times" gives me headache, so i opened the ThreadedKernel.java and then i comment the line were KThread.selfTest() is called for the ThreadedKernel and also i added the starter method of my class here :)public void selfTest() {
//KThread.selfTest();
//Semaphore.selfTest();
//SynchList.selfTest();
Prueba.start();
if (Machine.bank() != null) {
ElevatorBank.selfTest();
}
}
last edit 15/09/2011changed> line x,x,x, on the Prueba.java. thanks Juan carlos.
changed> grammar and spelling (attempt to improve)