miércoles, 14 de septiembre de 2011

What we did in NachOS

Knowing that User programs consists in Syscalls, we did the following:

(1) WE ADD 1 SYSCALL TO USERPROGR/SYSCALL.H


(2) WE ADD THE DECLARATION OF THAT SYSCALL IN THE SAME FOLDER



(3) WE WRITE SOME THINGS TEST/START.S FOLDER IN ASSAMBLER LANGUAGE -> CUADRADO



(4) WE ALSO ADD SOME CODE IN USERPROGR/MAKEFILE. WE WRITE OUR LITTLE CODE NAME, SO THE KERNEL COULD READ OUR PROGRAM.



(5) WHEN WE FINISHED ALL CHANGES WE NEEDED, WE WRITE A LITTLE CODE TO USE THOSE DECLARATIONS AND SYSTEM CALL..


(6) AFTER ALL WE DID WE REALIZE THAT NACHOS DIDN'T COMPILE AGAIN!! :C







SO, BECAUE OF THIS REASON, WE COULD JUST FIX OUR DINING PHILOSOPHERS PROBLEM IN JAVA. WE WERE ABLE TO AVOID THE DEADLOCK


THIS IS OUR SOLVED CODE:



IT WORKS :) EACH PHILOSOPHER SHARES IT FORK WITH THE OTHERS :)

martes, 13 de septiembre de 2011

Dining Philosophers

this is the code that we obtained in java

import java.util.*;
public class DinerThread extends Thread {
   public static final int numberOfThreads = 5;
   public static Object[] listOfLocks = new Object[numberOfThreads];
   public static char[] dinerTable = new char[4*numberOfThreads];
   public static char[] lockedDiner = new char[4*numberOfThreads];
   public static Random randomGenerator = new Random();
   public static int unitOfTime = 500;
   private int threadIndex;
   public static void main(String[] a) {
      for (int i=0; i<numberOfThreads; i++) listOfLocks[i] =
         new Object();
      for (int i=0; i<numberOfThreads; i++) {
         dinerTable[4*i] = '|';
         dinerTable[4*i+1] = ' ';
         dinerTable[4*i+2] = '-';
         dinerTable[4*i+3] = ' ';
         lockedDiner[4*i] = ' ';
         lockedDiner[4*i+1] = '|';
         lockedDiner[4*i+2] = '=';
         lockedDiner[4*i+3] = ' ';
      }
      for (int i=0; i<numberOfThreads; i++) {
         Thread t = new DinerThread(i);
         t.setDaemon(true);
         t.start();
      }
      String lockedString = new String(lockedDiner);
      System.out.println("The diner table:");
      long step = 0;
      while (true) {
         step++;
         System.out.println((new String(dinerTable))+"   "+step);
         if (lockedString.equals(new String(dinerTable)))
            break;
         try {
            Thread.sleep(unitOfTime);
         } catch (InterruptedException e) {
            System.out.println("Interrupted.");
         }
      }
      System.out.println("The diner is locked.");
   }
   public DinerThread(int i) {
      threadIndex = i;
   }
   public void run() {
      while (!isInterrupted()) {
         try {
            sleep(unitOfTime*randomGenerator.nextInt(6));
         } catch (InterruptedException e) {
            break;
         }
         // Try to get the chopstick on the left
         Object leftLock = listOfLocks[threadIndex];
         synchronized (leftLock) {
              int i = 4*threadIndex;
              dinerTable[i] = ' ';
              dinerTable[i+1] = '|';
            dinerTable[i+2] = '=';
            try {
               sleep(unitOfTime*1);
            } catch (InterruptedException e) {
               break;
            }
            // Try to get the chopstick on the right
            Object rightLock =
               listOfLocks[(threadIndex+1)%numberOfThreads];
            synchronized (rightLock) {
               dinerTable[i+2] = 'o';
               dinerTable[i+3] = '|';
               dinerTable[(i+4)%(4*numberOfThreads)] = ' ';
               try {
                  sleep(unitOfTime*1);
               } catch (InterruptedException e) {
                  break;
               }
               dinerTable[i] = '|';
               dinerTable[i+1] = ' ';
               dinerTable[i+2] = '-';
               dinerTable[i+3] = ' ';
               dinerTable[(i+4)%(4*numberOfThreads)] = '|';
            }
         }
      }
   }
}





There are several ways to solve the problem of assigning a time she would be given to each philosopher so he can eat and not starve. Another is that each philosopher has his assigned shift to eat or think, and so could be justice in shifts of food and starve any