Semaphore
View more presentations from naniix21_3
Ext3 vs FAT |
System.out.println(" acquire successful by thread: "+ KThread.currentThread());
System.out.println(" release successful by thread: "+ KThread.currentThread());
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/2011