miércoles, 14 de septiembre de 2011

Practical Presentation 1

This is the explanation for the practical part 1.

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/2011
changed> line x,x,x, on the Prueba.java. thanks Juan carlos.
changed> grammar and spelling (attempt to improve)

lunes, 12 de septiembre de 2011

Here's the slides of the Theorical Part 1


User program "Elevator" (Pseudocode)

Here is the pseudocode for an user program we are gonna try to develop for NachOS, it may have some flaws by now but we hope to make it work as it should :)

Implementing locks using Semaphores

Heres the algorithm of how we'll implement locks using semaphores. and actually a lock it is really a binary semaphore. here's the code.

domingo, 11 de septiembre de 2011

Implement a condition variable using semaphores.

Class: ConditionSemaphores

Lock cl;
SemaphoreLinkedList waitQueue;


function: Constructor(Lock *cl)


function: sleep


function: wake


function: wakeall
Reference: we use the code of the Condition variables of the nachos (java version)

Consumer-Producer (Unbounded buffer)

For the problem of the consumer-producer there are many solutions, with the condition that the buffer where unbounded we reach to the next one.

function main

function producer

function consumer


How this algorithm will solve the problem? Well in this solution the producer is free to create items at any time, the cosumer doesn't care at all.
But if the consumer want to take an item, he need to check the amount of items in stock, if the amount is zero, he will do nothing.