Os Practical Assignment 1

Preview:

Citation preview

Emmanuel Alejandro García Solís - 1450138 Maximiliano Hernández Castillo - 1453557Adán de Jesús Silva Cuéllar - 1462847

Team OS-Project-EVA

http://os-projecteva.blogspot.com/

Operating SystemsFirst Practical Assignment

Lock::Lock(const char* debugName) {    name = debugName;    semaphore = new Semaphore("lock", 1);    lockHolder = NULL;}

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

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

Implementation of Lockssynch.cc synch.h

class Lock { public:

Lock(const char*debugName); ~Lock(); void Acquire(); void Release(); bool IsHeldByCurrentThread() bool tryAcquire();

private: const char* name;Thread *lockHolder;

Semaphore *semaphore;};

Implementation of Lockssynch.cc synch.h

class Lock { public:

Lock(const char*debugName); ~Lock(); void Acquire(); void Release(); bool IsHeldByCurrentThread() bool tryAcquire();

private: const char* name;Thread *lockHolder;

Semaphore *semaphore;};

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; }}

/*Almost the same than Acquire(), except that, if someone has the lock, it will return immediately instead of waiting*/

Implementation of C. V.synch.cc

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

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; }

/*Queue containing allocatedSemaphores for each waiting thread*/

/*1. Checks if the current thread has the lock.2. Creates a semaphore, initially 0.3. Adds the semaphore to the queue.4. Releases the lock, and goes to sleep until someone sends a Signal.5. When someone sends a Signal, reaquires the lock and deletes the semaphore*/

Implementation of C. V.synch.cc

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);    }}

/*Checks if the the current thread has the lock*/

/*If it’s true, removes the last semaphore from the wait queue, assigns it to the waiter semaphore, and calls V, to wake him up*/

/* Wake up all threads waiting on this condition until the wait queue is empty*/

Dining philosophers

Dining philosophers

In computer science, the dining philosophers problem is an example problem often used in concurrent algorithm design to illustrate synchronization issues and techniques for resolving them.

Deadlock

Refers to a specific condition when two or more processes are each waiting for the other to release a resource, or more than two processes are waiting for resources in a circular chain.

some possible solutions

By cyclic turn

By Several turns.Established several turns. To make it clear, we suppose each philosopher has a token while is eating, when he finishes, hegives his token to the next philosopher at his right.

By Several turns

Established several turns. To make it clear, we suppose each philosopher has a token while is eating, when he finishes, hegives his token to the next philosopher at his right.

Gracias por su atencion

:)

Recommended