September 14, 2011

Assignment 1 - Practical Part

Screenshots of the code and its execution in the terminal. At the end I put the same code, but I can't show whit syntax highligther because the Internet in the UANL is very bad, and I need to modify the HTML code.

File synch.cc.


File threadtest.cc with our code.




We don't have any error after compile.


Screenshot of Producer-Consumer execution with semaphores.


Screenshot of Producer-Consumer execution with locks.


Some code:
Lock * L = new Lock("Lock");
void ConsumerLock(int which) {
  while (1) {
    if (disponibles > 0) {
      L->Acquire();
      disponibles--;
      printf("Thread %d >> Consumidor >> disponibles %d\n",
              which, disponibles);
      L->Release();
    }
    if (disponibles == 0) {
      currentThread->Finish();
    }
    currentThread->Yield();
  }
}
void ProducerLock(int which) {
  while (1) {
    if (disponibles < 15) {
      L->Acquire();
      disponibles++;
      printf("Thread %d >> Productor >> disponibles %d\n",
              which, disponibles);
      L->Release();
    }
    if (disponibles == 15) {
      currentThread->Finish();
    }
    currentThread->Yield();
  }
}
void ThreadTestLocks() {
  DEBUG('t', "Entra al ThreadTestLocks");
  printf("Locks\n");
  
  Thread *t = new Thread("Consumer");
  t->Fork((VoidFunctionPtr)ConsumerLock, (void *)1);

  Thread *tr = new Thread("Productor");
  tr->Fork((VoidFunctionPtr)ProducerLock, (void *)1);

  ProducerLock(0);
}

September 12, 2011