miércoles, 14 de septiembre de 2011

First Practical Assignment

We're using nachos 3.4 and 4.0, because we're looking the differences in the code and we are complementing nachos 3.4 with 4.0 code. 
Nachos 4.0 already has a basic structure of semaphores and locks.
We had several problems compiling, and probably broke nachos many times but finally we accomplished our goal (with a little help of the code already complemented in the Nachos 4.0 and Max, Emmanuel and Adan's Blog 

Here we show you the code we have in the Synch.cc part where we implemented locks


Lock::Lock(const char* debugName) {
  //parte agregada para candado
  semaphore = new Semaphore("lock", 1);
  lockHolder = NULL;
}
//Destructor
Lock::~Lock() {
  delete semaphore;
}
void Lock::Acquire() {
  semaphore->P();
  lockHolder = currentThread;
}
//checar esta parte que fue agregada
bool 
Lock::IsHeldByCurrentThread(){
  return lockHolder == currentThread;
}
void Lock::Release() {
  ASSERT(IsHeldByCurrentThread());
  lockHolder = NULL;
  semaphore->V();
}
bool Lock::tryAcquire(){
  if(lockHolder == NULL){
    semaphore->P();
    lockHolder = currentThread;
    return true;
  }
  else{
    return false;
  }
}
Condition::Condition(const char* debugName, Lock* conditionLock) { 
  name = debugName;
  waitQueue = new List <Semaphore *>;
}
Condition::~Condition() { 
delete waitQueue;
//}
void Condition::Wait(Lock *conditionLock) { 
  Semaphore *waiter;
  ASSERT(conditionLock->IsHeldByCurrentThread());
    
  waiter = new Semaphore("condition", 0);
  waitQueue->Append(waiter);
  conditionLock->Release();
  waiter->P();
  conditionLock->Acquire();
  delete waiter;
}
void Condition::Signal(Lock *conditionLock) { 
  Semaphore *waiter;
  ASSERT(conditionLock->IsHeldByCurrentThread());
  if(!waitQueue->IsEmpty()){
    waiter = waitQueue->Remove();
    waiter->V();
  }
}
void Condition::Broadcast(Lock  *conditionLock) { 
  while(!waitQueue->IsEmpty()){
    Signal(conditionLock);
  }
}








Now we show you the code we have in the Synch.h part, where we also implemented locks


//En esta parte es donde se agregan los candados y condition variable!!
//Constructor
Lock::Lock(const char* debugName) {
  //parte agregada para candado
  semaphore = new Semaphore("lock", 1);
  lockHolder = NULL;
}


//Destructor
Lock::~Lock() {
  delete semaphore;
}


void Lock::Acquire() {
  semaphore->P();
  lockHolder = currentThread;
}


//checar esta parte que fue agregada
bool
Lock::IsHeldByCurrentThread(){
  return lockHolder == currentThread;
}


void Lock::Release() {
  ASSERT(IsHeldByCurrentThread());
  lockHolder = NULL;
  semaphore->V();
}


bool Lock::tryAcquire(){
  if(lockHolder == NULL){
    semaphore->P();
    lockHolder = currentThread;
    return true;
  }
  else{
    return false;
  }
}


Condition::Condition(const char* debugName, Lock* conditionLock) {
  name = debugName;
  waitQueue = new List <Semaphore *>;
}


//Condition::~Condition() {
//delete waitQueue;
//}


void Condition::Wait(Lock *conditionLock) {
  Semaphore *waiter;
  ASSERT(conditionLock->IsHeldByCurrentThread());
 
  waiter = new Semaphore("condition", 0);
  waitQueue->Append(waiter);
  conditionLock->Release();
  waiter->P();
  conditionLock->Acquire();
  delete waiter;
}


void Condition::Signal(Lock *conditionLock) {
  Semaphore *waiter;
  ASSERT(conditionLock->IsHeldByCurrentThread());


  if(!waitQueue->IsEmpty()){
    waiter = waitQueue->Remove();
    waiter->V();
  }
}


void Condition::Broadcast(Lock  *conditionLock) {
  while(!waitQueue->IsEmpty()){
    Signal(conditionLock);
  }
}






We did a lot of modifications in the nachos code trying to implement the producer consumer problem but we broke it several times. 






(Screenshots)
(Code)
(Video)

4 comentarios:

  1. Por lo que veo el codigo es de la version 3.4
    En la funcion Lock::Lock(const char* debugName)
    debugName no se está llevando a ningun lado, creo que sale de sobra la variable, o podrias guardarla para el nombre del lock.

    Cuando usan CurrentThread no lo hacen llamando al kernel porque esto solo está habilitado en la version 4.0, como le hicieron?

    Por cierto dicen que lo primero es de synch.cc y lo demás de synch.h, yo los veo "algo" parecidos los codigos, en que difieren?

    ResponderEliminar
  2. Por cierto se pierden un poco las letras azules con el fondo :P

    ResponderEliminar
  3. Es verdad lo que dice Isaias :(
    Posiblemente por eso su nachOS se rompía en cada compilación, en el Synch.h solo van prototipos de funciones y declaraciones de variables.
    En el Synch.cc es la implementación completa del código.

    Intenten mover eso y serán felices de nuevo :).

    ResponderEliminar
  4. Sería bueno usar syntax highlighter y evitar capturas de pantalla en esta clase. Punto extra para Isaias.

    ResponderEliminar